我试图弄清楚如何为我的UITextField调用UITextFieldDelegate方法。这是我用来创建控制器和文本字段的一些代码:
@interface FirstViewController : UIViewController <UITextFieldDelegate> {
UITextField *fieldNumeroAppoggi;
}
@property (nonatomic,strong) UITextField *fieldNumeroArgomenti;
fieldNumeroAppoggi = [[UITextField alloc] initWithFrame:CGRectMake(210, 40, 50, 20)];
fieldNumeroAppoggi.borderStyle = UITextBorderStyleRoundedRect;
fieldNumeroAppoggi.tag = 1;
[self.dettagli addSubview:fieldNumeroAppoggi];
在我的控制器中,如果我实现了委托方法
- (BOOL) textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
永远不会被召唤。如果我设置fieldNumeroAppoggi.delegate = self;
,一切正常。如果我创建其他UITextField
个对象,是否必须为每个对象设置委托?
答案 0 :(得分:3)
假设我理解你...是的,一个对象可以是多个控件的委托。如果你需要根据发送消息的控件做不同的事情,你可以在委托处理程序中处理它。您可以将发件人与特定对象进行比较,也可以使用tag
属性来标识特定实例。
示例:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
BOOL shouldReturn = YES;
if (textField == fieldNumeroAppoggi) {
// do something
}
else if (textField == aDifferentTextField) {
// do something different
// maybe this one shouldn't return
shouldReturn = NO;
}
return shouldReturn;
}