使用委托以编程方式关闭文本字段键盘
我有一个这样的 viewController:
@interface RootViewController : UIViewController
然后我在 viewController.m 中以编程方式添加了一个文本字段:
UITextField*textFieldRounded=[[UITextField alloc] initWithFrame:frame];
textFieldRounded.delegate = self;
在同一个 .m
中我已经尝试了所有这些:
- (void)textFieldDidEndEditing:(UITextField *)textField{
[textField resignFirstResponder];
NSLog(@"returned?");
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
NSLog(@"returned?");
return YES;
}
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField{
[textField resignFirstResponder];
NSLog(@"returned?");
return YES;
}
当我单击文本字段(位于 viewController 的子视图中)时,键盘显示正常。但按“完成”没有任何作用。 NSLog 语句永远不会出现,因此这些方法永远不会被调用。有什么想法吗?
I have a viewController with this:
@interface RootViewController : UIViewController <UITextFieldDelegate>{
Then I have a textfield added programmatically like this in viewController.m:
UITextField*textFieldRounded=[[UITextField alloc] initWithFrame:frame];
textFieldRounded.delegate = self;
In the same .m
i have tried all of these:
- (void)textFieldDidEndEditing:(UITextField *)textField{
[textField resignFirstResponder];
NSLog(@"returned?");
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
NSLog(@"returned?");
return YES;
}
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField{
[textField resignFirstResponder];
NSLog(@"returned?");
return YES;
}
The keyboard appears fine when I click the text field, which is in a subview of the viewController. but pressing Done does nothing. The NSLog
statements never appear, so these methods are never getting called. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许我只是几乎没有这个问题,但我所做的是将点击手势识别器对象附加到视图,创建了一个名为“清除键盘:”的实例方法,并在函数内部放置了 [whateverItIsThatOpenedTheKeyboard resignFirstResponder];就是这样。也许你问的问题完全不同,但我想我会尽力提供帮助:)
I just barely had this question, maybe, but what I did was attached a tap gesture recognizer object to the view, made a instance method called clear keyboard:, and inside the function I put [whateverItIsThatOpenedTheKeyboard resignFirstResponder]; and that's it. Maybe you asking something totally different, but I thought I would try and help :)
将 .h 文件中的 UITextField 声明为具有属性的 IBOutlet,并将其合成到 .m 文件中。
Declare the UITextField in .h file as an IBOutlet with property and synthesize it in .m file.
使用此委托方法
将此委托方法添加到您的代码中并再次运行您的应用程序。
Use this delegate method
Add this delegate method to your code and run your app once again..