如何让键盘在输入文字后消失?

发布于 2024-08-29 19:45:54 字数 1162 浏览 3 评论 0原文

我的视图中有两个文本字段。我是用IB做的。我的问题是

  1. 在 textField1 中输入文本后,我在 textField2 中输入文本。当我再次单击 textField1 时,之前的文本会在 textField1 中消失。

  2. 在两个文本字段中输入文本后,我需要键盘消失。但是,即使我触摸键盘布局中的返回键或触摸文本字段外部的屏幕,键盘也不会消失。

我怎样才能做到这一点。 谢谢。

- (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

  1. 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.

  2. 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 技术交流群。

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

发布评论

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

评论(2

缪败 2024-09-05 19:45:54

对于问题 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.

初熏 2024-09-05 19:45:54

我得到了结果。我的错误第一件事是我没有在界面中添加 UITextFieldDelegate。剩下的我做了代码的更改。

- (void)viewDidLoad {
    self.title = @"Edit";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Save" style:UIBarButtonItemStyleDone target:self action:@selector(save)];

    [nameField becomeFirstResponder];
    nameField.delegate = self;
    descriptionField.delegate = self;

    [super viewDidLoad];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {

    NSLog(@"Return Key Pressed");
    if (textField == nameField) {
        [nameField resignFirstResponder];
        [descriptionField becomeFirstResponder];
    }
    else if(textField == descriptionField){
    [descriptionField resignFirstResponder];
    }
    return YES;
}

在 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.

- (void)viewDidLoad {
    self.title = @"Edit";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Save" style:UIBarButtonItemStyleDone target:self action:@selector(save)];

    [nameField becomeFirstResponder];
    nameField.delegate = self;
    descriptionField.delegate = self;

    [super viewDidLoad];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {

    NSLog(@"Return Key Pressed");
    if (textField == nameField) {
        [nameField resignFirstResponder];
        [descriptionField becomeFirstResponder];
    }
    else if(textField == descriptionField){
    [descriptionField resignFirstResponder];
    }
    return YES;
}

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.

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