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

发布于 2024-11-18 21:43:06 字数 1189 浏览 2 评论 0原文

我有下面的代码,但我无法弄清楚为什么 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

想念有你 2024-11-25 21:43:06

从您发布的代码来看,我认为您没有在标头中声明出口:

IBOutlet UITextField *login;
IBOutlet UITextField *password;

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

From the code that you've posted, it does not appear to me that you have declared the outlets in your header:

IBOutlet UITextField *login;
IBOutlet UITextField *password;

Be sure to declare them and set up the connections in the IB.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文