keyboard done key action swift iOS doesn't work

后端 未结 3 1696
温柔的废话
温柔的废话 2020-12-28 13:19

I\'m new in stackoverflow, I have a problem with new swift code. I have custom the return button on keyboard with \"Done\", but when I tap on it, don\'t befall anything... H

相关标签:
3条回答
  • 2020-12-28 13:42
    textField.delegate = self
    

    can be replaced by enter image description here

    This will create the necessary connections between your View, its component and will make the textFieldShouldReturn method work as expected.

    0 讨论(0)
  • 2020-12-28 13:50

    You need to implement delegate method which is called when you hit done button:

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }
    

    You also need to conform to UITextFieldDelegate protocol:

    // I assume you override UIViewController class. If not add UITextFieldDelegate to your class
    class MyViewController: UIViewController, UITextFieldDelegate
    

    The last thing is set up your class to be a text field delegate:

    textField.delegate = self
    
    0 讨论(0)
  • 2020-12-28 14:00

    The protocol methods have new signatures (Swift 4.1). IE:

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }
    

    As the protocol methods are optional, using a wrong signature will silently fail.

    0 讨论(0)
提交回复
热议问题