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

我如何知道键盘是数字,Twitter,电子邮件等?

编辑:有没有办法检测键盘types而不使用sockets?

考虑到你在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! } } } 

确保他们的委托连接到ViewController,以编程方式:

 tfEmail.delegate = self tfPassword.delegate = self 

或从界面生成器。

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

为给定的基于文本的视图显示的键盘types。 与keyboardType属性一起使用。

那么UITextView呢?

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

也,

如果你检查键盘types的主要目的只是为了识别当前响应的textField / textView,我build议直接检查一下:

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

有关===运算符的更多信息,您可能需要检查这个问题/答案 。

希望这有助于。

除了艾哈迈德·F的伟大的答案,这是我随时获得当前键盘types的方法:


第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的variables,如下所示:

 var keyboardType: UIKeyboardType? = nil 

然后,每当一个新的textField开始编辑时就改变它:

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

第3步:创build并调用如下所示的函数:

 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语句的键盘types,可以将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返回no current keyboard

让我知道这是否有帮助!