如何让键盘在输入文字后消失?
我的视图中有两个文本字段。我是用IB做的。我的问题是
在 textField1 中输入文本后,我在 textField2 中输入文本。当我再次单击 textField1 时,之前的文本会在 textField1 中消失。
在两个文本字段中输入文本后,我需要键盘消失。但是,即使我触摸键盘布局中的返回键或触摸文本字段外部的屏幕,键盘也不会消失。
我怎样才能做到这一点。 谢谢。
- (void)viewDidLoad
{
self.title = @"Edit";
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Save" style:UIBarButtonItemStyleDone target:self action:@selector(save)];
//[nameField becomeFirstResponder];
nameField.clearsOnBeginEditing = NO; //First text field
[nameField resignFirstResponder];
//[descriptionField becomeFirstResponder];
descriptionField.clearsOnBeginEditing = NO; //second text dield
[descriptionField resignFirstResponder];
[super viewDidLoad];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if (textField == nameField) {
[nameField resignFirstResponder];
}
else if(textField == descriptionField){
[descriptionField resignFirstResponder];
}
return YES;
}
我按照上面的方法做了,但是键盘还是没有消失。在 textField1 中输入文本后,当我按回车键时,光标不会转到 textField2。我怎样才能让它发挥作用?
谢谢。
I have two text fields in my view. I did it using IB. My problems are
After entering the text in textField1 I am entering text in textField2. When I click in textField1 again the previous text is disappeared in the textField1.
After entering the text in both the textFields, I need the keyboard to disappear. But, even I touched the return key in the keyboard layout or I touched the screen outside the text field the keyboard is not disappearing.
How can I make this.
Thank you.
- (void)viewDidLoad
{
self.title = @"Edit";
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Save" style:UIBarButtonItemStyleDone target:self action:@selector(save)];
//[nameField becomeFirstResponder];
nameField.clearsOnBeginEditing = NO; //First text field
[nameField resignFirstResponder];
//[descriptionField becomeFirstResponder];
descriptionField.clearsOnBeginEditing = NO; //second text dield
[descriptionField resignFirstResponder];
[super viewDidLoad];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if (textField == nameField) {
[nameField resignFirstResponder];
}
else if(textField == descriptionField){
[descriptionField resignFirstResponder];
}
return YES;
}
I did in the above way but the keyboard is still not disappearing. And after entering the text in textField1 when I press return key the cursor is not going to textField2. How can I make it work ?
Thank You.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于问题 1,请检查文本字段的
clearsOnBeginEditing
属性。对于问题 2,您可以将
resignFirstResponder
消息发送到文本字段。请参阅 管理键盘 部分nofollow noreferrer">UITextField 文档。
For problem 1, check the
clearsOnBeginEditing
property of the text field.For problem 2, you can send the
resignFirstResponder
message to the text field.See Managing Keyboard section in the UITextField documentation.
我得到了结果。我的错误第一件事是我没有在界面中添加 UITextFieldDelegate。剩下的我做了代码的更改。
在 textField1 中输入文本后,如果我触摸 return,光标将转到 textField2。它运行良好。但是,当我触摸屏幕上的文本字段时,键盘并没有消失。
谢谢。
I got the result. First thing in my mistake is I did not add UITextFieldDelegate in interface. And the remaining I did the changes in code.
After entering the text in textField1 if I touch return the cursor goes to textFeild2. It is working good. But when I touch the screen out of text field the keyboard is not disappearing.
Thank you.