How do I get the return key to perform the same action as a button press in Swift?

前端 未结 5 2005
一向
一向 2020-12-12 23:42

I want to know how you allow an action to be made by either pressing the return key on the software keyboard or by tapping a UIButton.

The UI button is already set u

5条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-13 00:06

    Swift 4.2 :

    Other approach for the textfield created programmatically and doesn't need delegate :

         MyTextField.addTarget(self, action: #selector(MyTextFielAction)
                                   , for: UIControl.Event.primaryActionTriggered)
    

    And then perform your action like below :

        func MyTextFielAction(textField: UITextField) {
           //YOUR CODE can perform same action as your UIButton
        }
    

提交回复
热议问题