gpt4 book ai didi

objective-c - 为什么 setDelegate 不适用于 UITextFieldDelegate,而是通过 Interface Builder 工作

转载 作者:行者123 更新时间:2023-12-01 17:01:19 27 4
gpt4 key购买 nike

我有下面的代码,我不知道为什么 textFieldShouldReturn方法没有被调用。当我使用 IB 创建到委托(delegate)的连接时,它可以工作,但是当以编程方式完成时,日志语句不会显示。我在这里做错了什么?

其次,在 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;
}

最佳答案

从您发布的代码来看,在我看来您没有在标题中声明网点:

IBOutlet UITextField *login;
IBOutlet UITextField *password;

请务必声明它们并在 IB 中设置连接。

关于objective-c - 为什么 setDelegate 不适用于 UITextFieldDelegate,而是通过 Interface Builder 工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6617906/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com