2017-02-09 30 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,在這種情況下。

讓我知道這是否有幫助!