2011-08-12 36 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)文本框,我引用的“编辑更改”事件的这个动作。像魅力一样工作。 –