2013-09-29 5 views
2

Итак, у меня есть серия текстовых полей в ячейках UITableView, которая находится в UIViewController (а не в контроллере табличного представления). Я могу редактировать текстовые поля, но методы делегата, такие как - (BOOL) textFieldShouldBeginEditing: (UITextField *) textField, не вызываются.Методы делегатов UITextField, которые не называются

мнение контроллер имеет свой делегат набор:

@interface NRSignUpViewController : UIViewController<UITextFieldDelegate, UIPickerViewDelegate, UIPickerViewDataSource, UITableViewDelegate, UITableViewDataSource> 

объявлены поля:

@property (nonatomic, strong) UITextField *firstName; 
@property (nonatomic, strong) UITextField *lastName; 
@property (nonatomic, strong) UITextField *email; 

и делегатов устанавливаются в viewDidLoad:

_firstName.delegate = self; 
_lastName.delegate = self; 
_email.delegate = self; 

Вот cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath  *)indexPath { 
    UITableViewCell *cell = [table dequeueReusableCellWithIdentifier:@"Cell"]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
             reuseIdentifier:@"Cell"]; 
     cell.accessoryType = UITableViewCellAccessoryNone; 
    } 

    UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, table.frame.size.width-20, 30)]; 
    tf.textColor = [UIColor blackColor]; 
    tf.returnKeyType = UIReturnKeyNext; 
    switch (indexPath.row) { 
     case 0: 
      tf.placeholder = @"First name"; 
      tf.autocapitalizationType = UITextAutocapitalizationTypeWords; 
      _firstName = tf; 
      break; 
     case 1: 
      tf.placeholder = @"Last name"; 
      tf.autocapitalizationType = UITextAutocapitalizationTypeWords; 
      _lastName = tf; 
      break; 
     case 2: 
      tf.placeholder = @"Email address"; 
      tf.autocapitalizationType = UITextAutocapitalizationTypeNone; 
      tf.keyboardType = UIKeyboardTypeEmailAddress; 
      _email = tf; 
      break; 
     default: 
      break; 
    } 

    [cell.contentView addSubview:tf]; 
    return cell; 
} 

Любые идеи, что может отсутствовать?

+0

Для Swift: убедитесь, что делегат метода не в частном расширения; они все еще могут быть в нормальном расширении, хотя – iwasrobbed

ответ

3

Вы устанавливаете делегатов в viewDidLoad, но фактически не создаете текстовые поля до cellForRow....

+0

Вот и все - спасибо! Знал, что это должно было быть чем-то очевидным, что я не замечал. – nickd717

3

Поместите контрольную точку внутри своего метода viewDidLoad и проверьте, не являются ли ваши текстовые поля nil. Это потому, что объект еще не инициализирован. Вы должны установить делегаты внутри метода tableView:cellForRowAtIndexPath:, потому что на тот момент это когда они фактически выделены.

1

я думаю, что вы можете попробовать это:

[[NSNotificationCenter defaultCenter] 
addObserver:self 
selector:@selector(methodNothing) 
name:UITextFieldTextDidChangeNotification 
object:firstName]; 

и вы можете использовать: UITextFieldTextDidBeginEditingNotification или UITextFieldTextDidEndEditingNotification

+0

спасибо, хороший ответ –

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