Search code examples
objective-cios4keyboardreturnnsuserdefaults

call a method with the "Return" (Done) Button of the keyboard


is there anyboby who can give me an example method that is called by pressing the return button of the keyboard and saves the text of a textview (that was typed in before) in the nsuserdefaults?

thanks a lot :)


Solution

  • Make sure your UITextField has a return key type set to UIReturnKeyGo (this is for the image on the keyboard):

    theTextField.returnKeyType = UIReturnKeyGo;
    

    Then use this method to do what ever you want to do:

    - (BOOL) textFieldShouldReturn:(UITextField *)textField
    {
        // Tell the keyboard where to go on next / go button.
        if(textField == theTextField)
        {
            // do stuff
        }
    
        return YES;
    }
    

    To get the text from the textfield just call theTextField.text and save as you wish!

    Swift Version

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        // Tell the keyboard where to go on next / go button.
        if textField == theTextField {
            // do stuff
        }
    
        return true
    }