失去焦点时隐藏 iPhone 键盘

发布于 2024-12-07 17:12:09 字数 769 浏览 0 评论 0原文

使用我在网上读到的内容,人们说在应用程序的后台放置一个按钮,然后单击该按钮即可关闭键盘。然而,阅读其他一些人的文章,他们说辞去文本字段的第一响应者 - 这是否意味着文本字段已经控制了键盘并且在明确告知之前不会释放它?

我的 .h 有:

@interface SearchItems : UIViewController {

    IBOutlet UIButton *btnKeyboard;
    IBOutlet UITextField *txtWhat;
    IBOutlet UITextField *txtWhere;

}

@property (nonatomic, retain) IBOutlet UIButton *btnKeyboard;
@property (nonatomic, retain) IBOutlet UITextField *txtWhat;
@property (nonatomic, retain) IBOutlet UITextField *txtWhere;

-(IBAction)closeKeyboard;

@end

.m (删减)

@synthesize btnKeyboard, txtWhat, txtWhere;

-(IBAction)closeKeyboard {

    [txtWhere resignFirstResponder];
    [txtWhat resignFirstResponder];

}

哪个不起作用,有什么想法吗?

谢谢

汤姆

Using what I've read online people say put a button in the background of the app then when clicked that button handles closing the keyboard. However reading some other people they say resign first repsonder for the text field - does that mean the textfield has taken control of the keyboard and wont release it until specifically told to?

My .h has:

@interface SearchItems : UIViewController {

    IBOutlet UIButton *btnKeyboard;
    IBOutlet UITextField *txtWhat;
    IBOutlet UITextField *txtWhere;

}

@property (nonatomic, retain) IBOutlet UIButton *btnKeyboard;
@property (nonatomic, retain) IBOutlet UITextField *txtWhat;
@property (nonatomic, retain) IBOutlet UITextField *txtWhere;

-(IBAction)closeKeyboard;

@end

The .m (cut down)

@synthesize btnKeyboard, txtWhat, txtWhere;

-(IBAction)closeKeyboard {

    [txtWhere resignFirstResponder];
    [txtWhat resignFirstResponder];

}

Which isn't working, any ideas?

Thanks

Tom

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

南渊 2024-12-14 17:12:09

尝试使用委托方法来resignResponder:

 - (BOOL)textFieldShouldReturn:(UITextField *)textField
 {
    [textField resignFirstResponder];
    return YES;
}

除了这个之外,您还可以使用这个委托方法:

 - (void)textFieldDidEndEditing:(UITextField *)textField
 {
     [textField resignFirstResponder];
 }

这两种方法主要应该能够解决您的键盘隐藏问题,而不是使用背景按钮技术。

Try using the delegate methods to resignResponder:

 - (BOOL)textFieldShouldReturn:(UITextField *)textField
 {
    [textField resignFirstResponder];
    return YES;
}

Apart from this one you can also use this delegate method:

 - (void)textFieldDidEndEditing:(UITextField *)textField
 {
     [textField resignFirstResponder];
 }

These two methods mainly should be able to solve your keyboard hiding problem rather than using the background button technique.

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