【问题标题】:iOS: How to get the current visible keyboard type?iOS:如何获取当前可见的键盘类型?
【发布时间】:2017-02-09 07:46:56
【问题描述】:

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

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

【问题讨论】:

  • 使用UITextField.keyboardType 属性来确定类型。

标签: ios swift uikeyboard uikeyboardtype


【解决方案1】:

假设你在 ViewController 中有两个 textFields,你需要从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 枚举的一个实例:

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

UITextView 呢?

在使用 UITextViews 时应该应用完全相同的功能,但是您需要从 UITextViewDelegate 协议实现 textViewDidBeginEditing(_:) 方法,而不是实现 textFieldShouldBeginEditing。再次确保 textView 的 delegate 连接到 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

希望这会有所帮助。

【讨论】:

  • @Xcoder123 谢谢你的注意 :) 我认为它 - 不知何故 - 应该导致相同的目的,OP 可能需要添加一些逻辑来实现特定功能。
  • 是的,我已经在我的答案中添加了这个逻辑:-),我真的很想在没有键盘可见的情况下添加这个逻辑......而且我认为拥有一个单独的函数可以增加简单性,因为它更容易访问并且更易于定制
  • 有没有不用插座就能检测键盘类型的方法?
  • 当然!您可以通过编程方式创建一个并将其委托设置为self,而不是让 textField 作为 IBOultet
【解决方案2】:

除了 Ahmad F 的出色答案之外,这是我随时获取当前键盘类型的方法:


第一步:委托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

如果这有帮助,请告诉我!

【讨论】:

    猜你喜欢
    • 2012-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-06
    • 2014-10-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多