2011-08-12 34 views
0

時,我有這樣的觀點:如何獲得的UITextField值按鈕被點擊

enter image description here

和這些實現:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [self.tableLogin dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
             reuseIdentifier:CellIdentifier] autorelease]; 
     cell.accessoryType = UITableViewCellAccessoryNone; 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 

     if ([indexPath section] == 0) { 
      UITextField *textfield = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 165, 30)]; 
      textfield.adjustsFontSizeToFitWidth = YES; 
      textfield.textColor = [UIColor blackColor]; 
      textfield.backgroundColor = [UIColor whiteColor]; 
      textfield.autocorrectionType = UITextAutocorrectionTypeNo; 
      textfield.autocapitalizationType = UITextAutocapitalizationTypeNone; 
      textfield.textAlignment = UITextAlignmentLeft; 
      textfield.clearButtonMode = UITextFieldViewModeNever; 
      textfield.delegate = self; 

      if ([indexPath row] == 0) { 
       textfield.tag = 0; 
       textfield.placeholder = @"your username"; 
       textfield.keyboardType = UIKeyboardTypeDefault; 
       textfield.returnKeyType = UIReturnKeyNext; 
      } 
      else { 
       textfield.tag = 1; 
       textfield.placeholder = @"required"; 
       textfield.keyboardType = UIKeyboardTypeDefault; 
       textfield.returnKeyType = UIReturnKeyDone; 
       textfield.secureTextEntry = YES; 
      } 

      [textfield setEnabled:YES]; 
      [cell addSubview:textfield]; 
      [textfield release]; 
     } 
    } 
    if ([indexPath section] == 0) { 
     if ([indexPath row] == 0) { 
      cell.textLabel.text = @"Email"; 
     } 
     else { 
      cell.textLabel.text = @"Password"; 
     } 
    } 

    return cell;  
} 

- (BOOL)textFieldShouldReturn:(UITextField *)textField 
{ 
    switch (textField.tag) { 
     case 0: 
      self.username = textField.text; 
      break; 
     case 1: 
      self.password = textField.text; 
      break; 
    } 
    return true; 
} 

當無龍頭的用戶點擊登錄按鈕「完成」了近鍵盤不保存字段值。我怎樣才能解決這個問題?

回答

2

兩個選項:

保持這兩個文本框的引用,並在「完成」按鈕的操作方法拉他們text值。

或者,註冊UITextFieldTextDidChangeNotification並捕獲輸入。

+0

什麼是'UITextFieldTextDidChangeNotification'?我找不到任何東西。 –

+0

這是爲UITextField定義的通知名稱。你會註冊這樣說:'[NSNotificationCenter defaultCenter]的addObserver:自我選擇:@選擇(textFieldDidChange :)名稱:UITextFieldTextDidChangeNotification對象:文本字段]' –

+1

文檔* *是奇怪稀疏。第22本* PDF的*列出所有的類所支持的通知:http://developer.apple.com/library/ios/documentation/uikit/reference/UITextField_Class/UITextField_Class.pdf –

1

您可以使用- (void)textFieldDidEndEditing:(UITextField *)textField並有保存價值,除了敲擊回車鍵。

+0

我在我身邊做了一個改變,使用IBAction而不是void。 - (IBAction爲)textFieldDidEndEditing:(*的UITextField)文本框,我引用的「編輯更改」事件的這個動作。像魅力一樣工作。 –