iPhone Keyboard, how to make Return key work?

Discussion in 'Mac Programming' started by fenrus110, Mar 28, 2008.

  1. macrumors regular

    Joined:
    Mar 24, 2008
    #1
    I'm using a UITextField and I am guessing that I need to use UITextFieldDelegate somehow to make the Return key actually do something. (like exit the keyboard mode)

    I looked at:
    HelloWorldClassic, but the Return key doesn't do anything.
    UIShowCase, but it's just full of subclass upon subclass confusion.
    SQLLiteBooks, but again, full of subclass confusion.

    I'm a XCode newb, so can anyone explain or provide a quick and dirty example of how to make the keyboard work as intended?
     
  2. macrumors newbie

    Joined:
    Mar 29, 2008
    #2
    What I did was the following:

    When setting up the textfield, add an extra line that looks like:
    myTextField.returnKeyType = UIReturnKeyDone;

    You may also need the following line:
    myTextField.delegate = self;

    Then you need to add a function:

    // this helps dismiss the keyboard then the "done" button is clicked
    - (BOOL)textFieldShouldReturn:(UITextField *)textField
    {
    [textField resignFirstResponder];
    return YES;
    }

    That works for me.
     
  3. macrumors 65832

    benlangdon

    Joined:
    Jan 13, 2008
    #3
    ugh this just makes me want to go in the fetal position because of this program i need to write that is due monday and have put off for 3 weeks :(
     
  4. thread starter macrumors regular

    Joined:
    Mar 24, 2008
    #4
    @AndyQ

    Okay I see that. Now just one step further, what if I have two UITextField's? ie. username/password.

    Will the textFieldShouldReturnUITextField function be now listening to both UITextField's? Shouldn't they be separated?
     
  5. macrumors newbie

    Joined:
    Mar 29, 2008
    #5
    Do you want the same behaviour on both text fields?

    If so, you don't need to do anything else otherwise in the textFieldShouldReturn method just see which field the textField points to and handle accordingly

    e.g.
    if ( textField == usernameField )
    {
    ..
    }
     
  6. thread starter macrumors regular

    Joined:
    Mar 24, 2008
    #6
    hmm... massive if/else statements, seems like a similar event mechanism used in Java... (which I hate)
     
  7. macrumors 6502a

    Joined:
    Jul 25, 2001
    #7
    So your writing a program where the enter key when used in the 1st text box on the screen will perform one way and in the text input box directly below it the enter key will behave in an entirely different manner?

    D
     
  8. macrumors 6502

    Monkaaay

    Joined:
    Jun 19, 2006
    Location:
    Richmond, VA
    #8
    Always a bad idea. :)
     
  9. macrumors newbie

    Joined:
    Jul 22, 2002
    Location:
    Austin, Texas
    #9
    From UITextField to UITextField...

    I'm running into the same problem. In "normal" IB, the NSTextField responder chain can be easily set through "nextKeyView". But not in IB for iPhone Touch app interfaces. Sad...:(

    You can see a screenshot of my sample app at Cocoacoderblog.com.:apple:

    Here's what I want for events. First, upon touching UITextField nameField, a keyboard should come up that has not only "Next" for its return key, but maybe even "Return" instead but with "Previous" and "Next" on the navigation bar of the keyboard, much as one gets when using the iPhone's Safari in a multi-field setting.

    When I hit "Next", the focus goes from the first UITextField nameField to UITextField ageField. Upon completing the age, touch the "Return" key and the action should occur.

    I've tried calling the UITextField delegate textFieldShouldReturn to get this to work, but need to include something...just not sure what.
     
  10. macrumors newbie

    Joined:
    Jul 22, 2002
    Location:
    Austin, Texas
    #10
    was it something I said?

    Ok, where did everyone go?
     

Share This Page