在UITextField中重新签名键盘 [英] resign keyboard in UITextField

查看:128
本文介绍了在UITextField中重新签名键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在iOS 5上使用故事板,并且有一个简单的屏幕,上面有 UITextField 。我想在用户点击返回时关闭键盘。我遵循了其他建议,例如让我的控制器实现 UITextFieldDelegate 协议并实现 textFieldShouldReturn:,如下所示:

I'm working with storyboards on iOS 5 and have a simple screen that has a UITextField on it. I want to dismiss the keyboard when the user hits "Return". I followed other suggestions such as having my controller implements the UITextFieldDelegate protocol and implements textFieldShouldReturn: as follows:

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

但是我看到从未调用此方法。我已将故事板中我的视图的控制器设置为我的自定义 UIViewController

However I see that this method is never called. I have set the controller of my view in storyboarding to my custom UIViewController.

我还尝试了一个不同的实现,我创建了一个名为 dismissKeyboard IBAction c $ c>但奇怪的是我无法将 Did Exit 操作连接到我的 UITextField

I also tried a different implementation where I create an IBAction called dismissKeyboard but oddly I can't connect the Did Exit action to my UITextField.

任何想法?

更新:所以问题似乎是我使用的是UITextView而不是一个UITextField。我想要一个大区域来输入文本。当我将输入字段更改为UITextField时,它可以正常工作。有关如何使其与UITextView一起使用的任何想法?

Update : So the problem seems to be that I'm using a UITextView and not a UITextField. I wanted a large area for the text to be entered. When I change the entry field to a UITextField it works fine. Any ideas on how to make it work with a UITextView?

推荐答案

如果您希望键盘在点击返回键时关闭textview使用这个委托函数,

If you want the keyboard to dismiss when tap the return key for the textview use this delegate function,

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {

    if([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }

    return YES;
}

这篇关于在UITextField中重新签名键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆