O user
O user

Reputation: 97

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 ?

Upvotes: 0

Views: 1893

Answers (2)

EBDOKUM
EBDOKUM

Reputation: 1846

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
    }

Upvotes: 1

tfrank377
tfrank377

Reputation: 1858

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:.

Upvotes: 0

Related Questions