我在下面有以下代码,我无法弄清楚为什么textFieldShouldReturn
没有调用该方法。当我使用 IB 创建到委托的连接时,它可以工作,但是当以编程方式完成时,日志语句不会显示。我在这里做错了什么?
其次,在 中textFieldShouldReturn
,有的例子返回YES
,有的返回NO
。ios 文档指定返回YES
将提供默认实现。有人可以更详细地解释一下吗?
//.h
文件
@interface GoSocietyLoginController : UIViewController <UITextFieldDelegate> {
}
- (IBAction)textFieldDoneEditing:(id)sender;
- (BOOL)textFieldShouldReturn:(UITextField *)textField;
@end
//.m
文件
@interface GoSocietyLoginController ()
@property (nonatomic,retain) IBOutlet UITextField *login;
@property (nonatomic,retain) IBOutlet UITextField *password;
@end
@implementation GoSocietyLoginController
@synthesize login;
@synthesize password;
- (void)viewDidLoad
{
[super viewDidLoad];
[login setDelegate:self];
[password setDelegate:self];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
NSLog(@"Hello World");
if ([textField isEqual:login]) {
[password becomeFirstResponder];
}
return NO;
}