2017-02-09 31 views

回答

4

想想看,你在的ViewController拖文本框,您将需要从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 

或来自Interface Builder。

注意,您可以通过检查其keyboardType属性,这是UIKeyboardType枚举的实例认识当前文本框的键盘类型:

键盘的类型,以显示给定文本视图。与 一起使用keyboardType属性。

UITextView怎么样?

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

此外,

如果检查键盘类型的主要目的就是识别什么是当前的回应文本框/ 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

希望这对我有所帮助。

+0

@ Xcoder123感谢您的注意:)我认为它 - 某种程度上 - 应该导致相同的目的,OP可能需要添加一些逻辑来实现特定的功能。 –

+0

是的,我在我的答案中添加了这样的逻辑:-),并且我真的希望在没有键盘可见的情况下添加该逻辑......并且我认为有一个单独的函数会增加简单性,因为它更易于访问并且有点可定制 –

+0

有没有办法检测不使用插座的键盘类型? –

1

除了艾哈迈德˚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的方式工作:

添加一个名为键盘类型的变量,如下:

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,在这种情况下。

让我知道这是否有帮助!