简体   繁体   中英

entering text into UI textfield by pressing return button

let's say I want to enter some data into text field, text for example, I want to press done or return button in keyboard to enter data into UI text field.

how I can do that ?

There is a way to do this without implementing UITextFieldDelegate . You can add "target" for your UITextField which will call a method as user returns. Something like this:

    func init(){
        //Initialize everything else

        textField.addTarget(self, action: "textFieldReturned", forControlEvents: UIControlEvents.EditingDidEndOnExit)
    }

    //This method will be called after user "returns".
    //Note: Name this method whatever you like, just make sure it's
    //the same name you pass to 'addTarget' method as 'action:' parameter
    func textFieldReturned(){
        textField.resignFirstResponder() //Hides keyboard

        //Do everything you need after user presses "Done" button
    }

This should be as simple as using the UITextFieldDelegate to process when the return key has been pressed.

So you can adopt that protocol, assign the delegate on your UITextField and then process data in the delegate method textFieldDidEndEditing: when the return key is pressed. The delegate method I mentioned can be found here . Make sure you have enabled the processing of the return key by returning true from textFieldShouldReturn: .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM