iOS:如何获取当前可见的键盘类型? [英] iOS: How to get the current visible keyboard type?

查看:467
本文介绍了iOS:如何获取当前可见的键盘类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何确定键盘的类型是否为数字,Twitter,电子邮件等?

How do I find out if the keyboard is of type numeric, Twitter, email, etc...?

是否有一种无需使用插座即可检测键盘类型的方法?

edit: Is there a way to detect keyboard type without using an outlet?

推荐答案

考虑到您必须在ViewController中拖曳textField,您将需要实现

Consider that you have tow textFields in the ViewController, You will need to implement textFieldShouldBeginEditing method from UITextFieldDelegate protocol, as follows:

class ViewController: UIViewController, UITextFieldDelegate {
    @IBOutlet weak var tfEmail: UITextField!
    @IBOutlet weak var tfPassword: UITextField!

    func textFieldDidBeginEditing(_ textField: UITextField) {
        if textField.keyboardType == .emailAddress {
            // this is the tfEmail!
        }

        if textField.isSecureTextEntry {
            // this is tfPassword!
        }
    }
}

确保以编程方式将其代理人连接到ViewController:

Make sure their delegates are connected to the ViewController, programmatically:

tfEmail.delegate = self
tfPassword.delegate = self

或从Interface Builder中获取.

or from the Interface Builder.

请注意,您可以通过检查其来识别当前textField的键盘类型. keyboardType 属性,它是 UIKeyboardType 枚举的实例:

Note that you can recognize the keyboard type for the current textField by checking its keyboardType property, which is an instance of UIKeyboardType enum:

为给定的基于文本的视图显示的键盘类型.用于 keyboardType属性.

The type of keyboard to display for a given text-based view. Used with the keyboardType property.

UITextView怎么样?

What about UITextView?

使用UITextViews时应应用相同的确切功能,但您需要实现 UITextViewDelegate 协议中的"> textViewDidBeginEditing(_ :) 方法,而不是实现textFieldShouldBeginEditing.再次,确保textView的 delegate 已连接到ViewController.

The same exact functionality should be applied when working with UITextViews, but you need to implement textViewDidBeginEditing(_:) method from UITextViewDelegate protocol instead of implementing textFieldShouldBeginEditing. Again, make sure the delegate of the textView is connected to the ViewController.

也是

如果检查键盘类型的主要目的只是为了识别当前响应的textField/textView是什么,我建议直接进行检查:

If your main purpose of checking the keyboard type is just for recognizing what is the current responded textField/textView, I suggest to do a direct check:

class ViewController: UIViewController, UITextFieldDelegate, UITextViewDelegate {
    @IBOutlet weak var tfEmail: UITextField!
    @IBOutlet weak var tfPassword: UITextField!
    @IBOutlet weak var textViewDescription: UITextView!

    override func viewDidLoad() {
        super.viewDidLoad()

        tfEmail.delegate = self
        tfPassword.delegate = self

        textViewDescription.delegate = self
    }

    func textFieldDidBeginEditing(_ textField: UITextField) {
        if textField === tfEmail {
            // this is the tfEmail!
        }

        if textField === tfPassword {
            // this is tfPassword!
        }
    }

    func textViewDidBeginEditing(_ textView: UITextView) {
        if textView === textViewDescription {
            // this is description textview
        }
    }
}

有关 === 运算符的详细信息查看此问题/答案.

希望这会有所帮助.

这篇关于iOS:如何获取当前可见的键盘类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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