为什么 setDelegate 不适用于 UITextFieldDelegate,而是通过 Interface Builder 工作
我有下面的代码,但我无法弄清楚为什么 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;
}
I have the following code below, and I can't figure out why the textFieldShouldReturn
method is not being called. When I use IB to create the connection to the delegate, it works, but when done programmatically, the log statement is not being displayed. What am I doing wrong here?
Secondly, in the textFieldShouldReturn
, some examples return YES
, and some return NO
. The ios documentation specified that returning YES
would provide default implementation. Can someone please explain this in more detail?
//.h
file
@interface GoSocietyLoginController : UIViewController <UITextFieldDelegate> {
}
- (IBAction)textFieldDoneEditing:(id)sender;
- (BOOL)textFieldShouldReturn:(UITextField *)textField;
@end
//.m
file
@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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从您发布的代码来看,我认为您没有在标头中声明出口:
请务必声明它们并在 IB 中设置连接。
From the code that you've posted, it does not appear to me that you have declared the outlets in your header:
Be sure to declare them and set up the connections in the IB.