2011-05-06 71 views
1

我已經創建了包含用戶名和密碼的登錄頁面。我創建了一個自定義單元格,它有一個標籤和一個文本字段。我創建了一個分組表格視圖並使用了自定義單元格。其實我已經成功創建,但問題是,我如何獲取用戶名和密碼的文本字段值。 Bcoz我在這兩個領域只使用了一個文本框。我無法正確獲取文本字段值。我總是得到這兩個領域的最後價值。我如何獲得用戶名並正確傳遞單詞文本值?如何在iPhone的表格視圖中獲取文本字段值?

這裏我的示例代碼,

在自定義單元格,

@interface CustomCell : UITableViewCell { 

    UILabel *userLbl; 

    UITextField *userTxt; 

} 

@property (nonatomic, retain) IBOutlet UILabel *userLbl; 

@property (nonatomic, retain) IBOutlet UITextField *userTxt; 

@end 

在根視圖控制器,

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 

     NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:nil options:nil]; 

     for(id currentObjects in nibObjects) 
     { 
      if([currentObjects isKindOfClass:[CustomCell class]]) 
      { 
       cell = (CustomCell *) currentObjects; 
      } 
     } 
    } 
    if (indexPath.section == 0) { 

     if(indexPath.row == 0) 
     { 
      [[cell userLbl] setText:@"User Name:"]; 

      cell.userTxt.tag = 0; 

      //self.userName = [[cell userTxt] text]; 


     } 
     if (indexPath.row == 1) { 

      [[cell userLbl] setText:@"Pass Word:"]; 

      cell.userTxt.secureTextEntry = YES; 

      //self.passWord = [[cell userTxt] text]; 

      cell.userTxt.tag = 1; 

     } 

    } 

    cell.selectionStyle = UITableViewCellSelectionStyleNone; 


    // Configure the cell. 

    return cell; 
} 

-(IBAction) submitAction : (id) sender 
{ 

    self.userName = [[cell userTxt] text]; 

    self.passWord = [[cell userTxt] text]; 

    [cell.userTxt resignFirstResponder]; 

    NSLog(@"The user Name is %@", self.userName); 

    NSLog(@"The password is %@", self.passWord); 

    //Doesn't work 

/* UITextField *txtField = (UITextField*)[cell viewWithTag:0]; 

    self.userName = [txtField text]; 

    UITextField *txtField1 = (UITextField*)[cell viewWithTag:1]; 

    self.passWord = [txtField1 text]; 

    NSLog(@"The user Name is %@", self.userName); 

    NSLog(@"The password is %@", self.passWord);*/ 

} 

這裏我對圖像的屏幕快照,

enter image description here

請幫助我。

謝謝。

+0

我有同樣的問題 – Siva 2013-10-28 07:01:45

回答

0

我認爲在爲文本框設置標籤的方式中存在一些問題。我想都具有相同的標籤1.請查看是否改變,有助於

UPDATE

通過你怎麼可以在方法submitAction內進入細胞的方式。我認爲更容易將textfields中的文本分配給實例變量,如usernamepassword,並將其用於您的方法。對於每個文本字段和呼叫

+0

謝謝,現在我編輯了我的問題。 – Pugal 2011-05-06 11:41:41

+0

我無法獲得更新的答案,所以請詳細解釋一下。謝謝。 – Pugal 2011-05-06 11:52:19

+0

順便說一句,如何訪問方法submitAction中的單元格。我認爲更容易將文本字段中的文本分配給實例變量,說出用戶名和密碼,並在您的方法中使用它。 – visakh7 2011-05-06 11:53:37

2

使用標籤 -

UITextField *txtField = (UITextField*)[cell viewWithTag:tag]; 
NSString *text = [txtField text]; 

也7KV7是正確的,你是這兩個文本字段分配相同的標籤。

1

爲什麼不優雅地做呢?在你UITextFieldDelegate方法,像這樣做:

- (BOOL)textFieldShouldReturn:(UITextField *)textField { 
[textField resignFirstResponder]; 

    switch (textField.tag) { 
    case 0: 
     self.userName = textField.text; 
     break; 
    case 1: 
     self.passWord = textField.text; 
     break; 
    default: 
     break; 
} 
} 

這樣的話,你已經擁有的數據,當用戶點擊提交。只是一個想法。

3

如果您使用多個文本字段,那麼您可以使用textFieldShouldEndEditing方法,因爲textFieldShouldReturn方法只會從文本字段中獲取退出FirstResponder的字符串,因此您將錯過所有其他文本字段。這是我在最近的項目中使用的,當我有一個使用tableview和textfields構建的表單時。這讓我感到困惑了一段時間。

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField 
{ 
    switch (textField.tag) 
    { 
     case 0: 
      userName = textField.text; 
      break; 

     case 1: 
      passWord = textField.text; 
      break; 
    } 
    return YES; 
} 
相關問題