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

时间:2017-02-09 07:46:56

标签: ios swift uikeyboard uikeyboardtype

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

编辑:有没有办法在不使用插座的情况下检测键盘类型?

2 个答案:

答案 0 :(得分:4)

考虑到你在ViewController中有两个textFields,你需要从textFieldShouldBeginEditing协议实现UITextFieldDelegate方法,如下所示:

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:

tfEmail.delegate = self
tfPassword.delegate = self

或来自Interface Builder。

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

  

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

UITextView怎么样?

使用UITextViews时应该应用相同的功能,但是您需要从textViewDidBeginEditing(_:)协议实施UITextViewDelegate方法,而不是实现textFieldShouldBeginEditing。再次确保textView的委托连接到ViewController。

同时

如果检查键盘类型的主要目的只是识别当前响应的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
        }
    }
}

有关===运算符的详细信息,您可以查看this question/answers

希望这会有所帮助。

答案 1 :(得分:1)

除了 Ahmad F 之外,这是我获取当前键盘类型的方法:

第1步:委派UITextField

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

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
}

第2步:使用UITextField的方法:

添加一个名为keyboardType的变量,如下所示:

var keyboardType: UIKeyboardType? = nil

然后,每当新textField开始编辑时更改它:

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

第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())
}

根据具体情况输出:email / number / no current keyboard / default

如果您想查看if-else语句的键盘类型,可以将displayCurrentKeyboard()方法更改为:

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

就是这样!您可以使用以下用法在代码中的任何位置调用此方法:

 let keyboardString = self.getCurrentKeyboard()

注意:此方法还可以处理屏幕上没有键盘的情况,在这种情况下返回 no current keyboard

请告诉我这是否有帮助!