inputAccessoryView 关闭键盘 [英] inputAccessoryView Dismiss Keyboard

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

问题描述

我有一个文本视图停靠在视图的底部.但是,当用户在 commentTextView 之外点击时键盘不会关闭.

I have a textView docked to the bottom of the view. However, the keyboard wont dismiss when the user taps outside the commentTextView.

当前尝试:

import UIKit
class CommentsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet var commentBar: UIView!

    @IBOutlet var commentTextField: UITextField!

    override var inputAccessoryView: UIView {
        return commentBar
    }

    override func canBecomeFirstResponder() -> Bool {
        commentBar.removeFromSuperview()
        return true
    }

    func textFieldShouldReturn(textField: UITextField!) -> Bool {
        self.view.endEditing(true);
        return false;
    }

    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        self.view.endEditing(true);
        commentTextField.resignFirstResponder()
    }

推荐答案

根据 Apple 的文档,默认情况下,当用户在 UITextView 之外点击时键盘不会关闭.当您想通过调用 commentTextField.resignFirstResponder() 关闭键盘时,您确实需要以编程方式处理此问题.

According to Apple's documentation, the keyboard won't dismiss by default when the user taps outside of the UITextView. You do need to handle this programmatically when you want to dismiss the keyboard by calling commentTextField.resignFirstResponder().

UITextFiled 关于管理键盘

在您选择的时间关闭键盘是您的应用程序的责任.您可能会关闭键盘以响应特定的用户操作,例如用户点击用户界面中的特定按钮.要关闭键盘,请将 resignFirstResponder 消息发送到当前是第一响应者的文本视图.

It is your application’s responsibility to dismiss the keyboard at the time of your choosing. You might dismiss the keyboard in response to a specific user action, such as the user tapping a particular button in your user interface. To dismiss the keyboard, send the resignFirstResponder message to the text view that is currently the first responder.

用户可以通过多种方式隐藏键盘.

There are many ways for a user may hide the keyboard.

情况 1:一种是用户点击键盘上的返回按钮.这正是您的以下功能的用途:

Situation 1: one is when the user tap on the Return button on the keyboard. This is exactly what your following function is for:

func textFieldShouldReturn(textField: UITextField!) -> Bool {
    commentTextField.resignFirstResponder();
    return true;
}

但是主要的问题是上面的函数不会被调用,因为你忘记设置UITextFieldDelegate.简而言之,您需要将类定义更改为以下内容:

But the major problem is that the above function won't get called because you forget to set the UITextFieldDelegate. In short, you need to change the class definition to the following:

class CommentsViewController: UIViewController, UITextFieldDelegate /*Add This*/, UITableViewDelegate, UITableViewDataSource {
    // ....
}

您还必须在情节提要中或通过代码设置删除.与设置UITableViewDelegateUITableViewDataSource 相同.以下是在代码中设置它的示例:

You will also have to set the delete either in the storyboard or by code. It is the same as setting the UITableViewDelegate and UITableViewDataSource. By the following is an example of setting it in code:

override func viewDidLoad() {
    super.viewDidLoad()
    commentTextField.delegate = self;
}

情况 2:当用户点击表格视图时.您可以简单地实现以下 UITableViewDelegate 并且它应该可以工作.

Situation 2: when user tap on the table view. You can simply implement the following UITableViewDelegate and it should work.

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    commentTextField.resignFirstResponder();
}

情况 3:当用户点击背景视图时.这就是以下代码的用途.但是,当用户点击 table view 时,不会调用此函数.如果你想知道为什么,请参考响应者链了解更多详情.

Situation 3: when the user tap on a background view. This is what the following code is for. However, this function will not get called when user is tap on the table view. If you want to know why, please refer to the responder chain for more details.

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    // self.view.endEditing(true); // This line is redundant. 
    commentTextField.resignFirstResponder()
}

总而言之,没有快速简便的方法来关闭键盘说当用户在 textView 之外点击时".您确实需要根据需要处理所有不同的情况.

To sum up, there is no quick and easy way to dismiss the keyboard say "when the user taps outside the textView". You do need to handle all different situations according to your need.

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

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