当在UITextView上失去焦点时隐藏键盘 [英] Hiding the Keyboard when losing focus on a UITextView

查看:691
本文介绍了当在UITextView上失去焦点时隐藏键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个UITextView,我使用它允许用户提交一些文本。

So I have a UITextView that I'm using to allow the user to submit some text.

我的问题是,我似乎不知道如何允许用户通过点击远离UITextView来取消。

My problem is, I can't seem to figure out how to allow the user to 'Cancel' by tapping away from the UITextView.

推荐答案

简化 tuzzolotron的回答
如果以下插座已正确连接您的xib

Simplifying tuzzolotron's answer: Where the following outlet is properly connected in your xib

    IBOutlet UITextView *myTextView;

在视图控制器中使用:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [[event allTouches] anyObject];
    if ([myTextView isFirstResponder] && [touch view] != myTextView) {
        [myTextView resignFirstResponder];
    }
    [super touchesBegan:touches withEvent:event];
}

点击文本视图之外的视图控制器视图第一个响应者,关闭键盘。

A tap on the View Controller's View, outside of the Text View, will resign the first responder, closing the keyboard.

这篇关于当在UITextView上失去焦点时隐藏键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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