的UITextField委托方法不叫
所以我有一个UIViewController(不表视图控制器)内的生活一个UITableView细胞一系列的文本字段。我可以编辑文本字段,但像 - (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;
}
任何想法可能会丢失什么?
你设定viewDidLoad
代表而不是实际创建的文本字段,直到cellForRow...
。
他们仍然可以进行正常的扩展 - 就是这样 - 谢谢!知道它必须是我忽略的一些明显的东西。 – nickd717
将断点您viewDidLoad
方法内,并检查您的文本字段nil
。这是因为对象尚未初始化。您应该在tableView:cellForRowAtIndexPath:
方法中设置代表,因为在那个时候它们是实际分配的。
我想你可以试试这个:
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(methodNothing)
name:UITextFieldTextDidChangeNotification
object:firstName];
,你可以使用:UITextFieldTextDidBeginEditingNotification
或UITextFieldTextDidEndEditingNotification
谢谢,很好的回答 –
对于斯威夫特:确保委托方法不是私有扩展;尽管 – iwasrobbed