简体   繁体   English

iOS:如何获取当前可见的键盘类型?

[英]iOS: How to get the current visible keyboard type?

How do I find out if the keyboard is of type numeric, Twitter, email, etc...? 如何确定键盘的类型是否为数字,Twitter,电子邮件等?

edit: Is there a way to detect keyboard type without using an outlet? 编辑:有没有一种方法,无需使用插座即可检测键盘类型?

Consider that you have tow textFields in the ViewController, You will need to implement textFieldShouldBeginEditing method from UITextFieldDelegate protocol, as follows: 考虑到您在ViewController中有两个textField,您将需要通过UITextFieldDelegate协议实现textFieldShouldBeginEditing方法,如下所示:

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!
        }
    }
}

Make sure their delegates are connected to the ViewController, programmatically: 确保其委托以编程方式连接到ViewController:

tfEmail.delegate = self
tfPassword.delegate = self

or from the Interface Builder. 或通过界面生成器。

Note that you can recognize the keyboard type for the current textField by checking its keyboardType property, which is an instance of UIKeyboardType enum: 请注意,您可以通过检查当前textField的keyboardType属性(它是UIKeyboardType枚举的实例)来识别其键盘类型:

The type of keyboard to display for a given text-based view. 为给定的基于文本的视图显示的键盘类型。 Used with the keyboardType property. 与keyboardType属性一起使用。

What about UITextView ? UITextView呢?

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 . 使用UITextViews时,应使用相同的确切功能,但您需要从UITextViewDelegate协议中实现textViewDidBeginEditing(_ :)方法,而不是实现textFieldShouldBeginEditing Again, make sure the delegate of the textView is connected to the ViewController. 再次,确保textView的委托连接到ViewController。

Also, 也,

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

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

For more information about === operator you might want to check this question/answers . 有关===运算符的更多信息,您可能需要检查此问题/答案

Hope this helped. 希望这会有所帮助。

In addition to Ahmad F 's great answer, this is my approach of getting the current keyboard type, at any time: 除了Ahmad F的好答案之外,这是我随时获取当前键盘类型的方法:


Step 1: Delegate UITextField 步骤1:委派UITextField

class File: UIViewController, UITextFieldDelegate{//...}

Update viewDidLoad() to this: viewDidLoad()更新为:

@IBOutlet weak var normalTextField: UITextField!
@IBOutlet weak var numberTextField: UITextField!
@IBOutlet weak var emailTextField: UITextField!

override func viewDidLoad() {
    super.viewDidLoad()
    numberTextField.keyboardType = .numberPad
    normalTextField.keyboardType = .default
    emailTextField.keyboardType = .emailAddress
    numberTextField.delegate = self
    normalTextField.delegate = self
    emailTextField.delegate = self
}

Step 2: Working with UITextField 's methods: 步骤2:使用UITextField的方法:

Add a variable called keyboardType, as below: 添加一个名为keyboardType的变量,如下所示:

var keyboardType: UIKeyboardType? = nil

Then, change it whenever a new textField begins editing: 然后,每当新的textField开始编辑时就对其进行更改:

func textFieldDidBeginEditing(_ textField: UITextField) {
        keyboardType = textField.keyboardType
}
func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
        keyboardType = nil
        return true
}

Step 3: Create and call a function like below: 步骤3:建立并呼叫如下函数:

func getCurrentKeyboard() -> String{
    if keyboardType == nil{
        return "no current keyboard"
    }
    else if keyboardType == .numberPad{
        return "number"
    }
    else if keyboardType == .emailAddress{
        return "email"
    }
    else{
        return "default"
    }
}

@IBAction func displayCurrentKeyboard(_ sender: UIButton) {
       print(self.getCurrentKeyboard())
}

And this outputs: email / number / no current keyboard / default , depending on the case. 并根据情况输出: email / number / no current keyboard / default

If you want to check which type of keyboard it is with if-else statements, you can change your displayCurrentKeyboard() method to this: 如果要使用if-else语句检查哪种类型的键盘,可以将displayCurrentKeyboard()方法更改为:

@IBAction func displayCurrentKeyboard(_ sender: UIButton) {
      let keyboardString = self.getCurrentKeyboard()
      if keyboardString == "number"{
      //...
      }
      else if keyboardString == "email"{
      //...
      }
      else{
      //...
      }
}

And that's it! 就是这样! You can call this wherever you want in your code with this usage: 您可以使用以下用法在代码中的任何位置调用此方法:

 let keyboardString = self.getCurrentKeyboard()

NOTE: This method also handles the case of no keyboard visible on the screen, returning no current keyboard , in this case. 注意:此方法还可以处理在屏幕上看no current keyboard ,在这种情况下no current keyboard返回no current keyboard

Let me know if this helps! 让我知道这是否有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM