This is correct! Why won't it work?

Discussion in 'iOS Development' started by iPodTouchDev, May 2, 2009.

  1. iPodTouchDev

    iPodTouchDev New Member

    Joined:
    Jan 18, 2009
    Messages:
    74
    Likes Received:
    0
    Device:
    iPod touch
    Ok I am trying to have a BOOL that dismisses more than one UITextField.

    This should work, right? I've looked around and this is what is recommended, but when I use it, when the return button is pressed, nothing happens.

    This is the code I am using (Note - the name of the UITextFields are not the one's used, I am just demonstrating.

    Code:
    - (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
    if (theTextField == fieldOne)
    {
         [ fieldOne resignFirstResponder ];
    }
    else if (theTextField == fieldTwo)
    {
         [ fieldTwo resignFirstResponder ];
    }
    else if (theTextField == fieldThree)
    {
         [ fieldThree resignFirstResponder ];
    }
    return YES;
    }
  2. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    Why not just use [theTextField resignFirstResponder]? There's no need fir all those if statements.

    Also, make sure that your textfield's delegate is set to whatever object contains the provided code. And, make sure that object conforms to the UITextField protocol (<UITextFieldDelegate>).
  3. Chicken

    Chicken Retired Moderator

    Joined:
    Feb 17, 2008
    Messages:
    2,494
    Likes Received:
    7
    Device:
    iPhone 5S
    Just a note that if you set the delegate in Interface Builder, you don't need to add <UITextFieldDelegate>

Share This Page