2011-08-12 13 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

два варианта:

Держите ссылку на обоих TextFields и подтягивания их text значения в «Done» метод действия кнопки.

В качестве альтернативы зарегистрируйтесь для UITextFieldTextDidChangeNotification и возьмите вход как только это произойдет.

+0

Что такое 'UITextFieldTextDidChangeNotification'? Я ничего не могу найти. –

+0

Это имя уведомления, определенное для UITextField. Вы зарегистрировались бы так: '[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector (textFieldDidChange :) name: UITextFieldTextDidChangeNotification object: textField];' –

+1

Документы * * странно разрежены. Страница 22 из этого * PDF * перечисляет все уведомления, поддерживаемые классом: http://developer.apple.com/library/ios/documentation/uikit/reference/UITextField_Class/UITextField_Class.pdf –

1

Вы можете использовать - (void)textFieldDidEndEditing:(UITextField *)textField и сохранить свои значения там, кроме того, чтобы нажать клавишу возврата.

+0

Я сделал изменения на моей стороне, чтобы использовать IBAction вместо пустоты. - (IBAction) textFieldDidEndEditing: (UITextField *) textField, и я ссылался на событие «Редактирование изменено» для этого действия. Работал как шарм. –

Смежные вопросы