Handling the Done key on the keyboard.

Discussion in 'iOS Programming' started by seVor, Aug 20, 2008.

  1. macrumors newbie

    Joined:
    Aug 20, 2008
    #1
    How can I execute code after the user hits the done key.
     
  2. macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #2
    Huzzah!

    A question I can answer!

    So now the flow of information won't be so one-way!

    You need to set your controller object to be the delegate of the text field first.

    So... for example...

    Code:
    [i]textField[/i].delegate = self
    I put that in the awakeFromNib method for my controller... and you need to do that for each text field.

    Then you also need a method called this:

    Code:
    - (BOOL) textFieldShouldReturn: (UITextField *) textField
    {
    // Insert the code you want to execute here.
    }
    [/code]

    Finally, in the header file of the object you want to be the controller, you need

    @interface name of your controller object : NSObject <UITextFieldDelegate>

    so that you won't get errors about not having the right protocols implemented.

    Oh... one last thing... if you're having multiple text fields that you want to do different things... you might want an if-else in your code.

    Like...

    Code:
    - (BOOL) textFieldShouldReturn: (UITextField *) textField
    {
    if (textField == nameField)
    {
    // Code that you want it to do if nameField has just had its done button hit.
    }
    
    else if (textField == otherField)
    {
    // Code that you want it to do if the otherField has just had its done button hit.
    
    ... Hopefully you understood that all... and hopefully I told you the right things...
     
  3. thread starter macrumors newbie

    Joined:
    Aug 20, 2008

Share This Page