Question: Hiding the keyboard when a UITextField is empty

Discussion in 'iOS Programming' started by arubinst, Oct 10, 2008.

  1. arubinst macrumors 6502

    Joined:
    May 26, 2008
    Location:
    Lausanne - Switzerland
    #1
    Hi,

    I've googled around for this problem but I find nothing. I'm wondering if I'm the only one...

    I have setup a view with 2 TextFields in IB. Delegates of the TextFields are correctly set and the ViewController implements the UITextFieldDelegate protocol.

    I would like to dismiss the keyboard with the "Done" key, but the key is grayed out while the TextField is empty. Once I type any character, the Done key is enabled and I can dismiss the keyboard with:

    Code:
    - (BOOL)textFieldShouldReturn:(UITextField *)textField {
    	NSLog(@"textFieldShouldReturn");
    	[textField resignFirstResponder];
    	return YES;
    }
    So, what am I missing? Why is the "Done" key disabled when the TextField is empty?

    By the way, I looked at the UICatalog sample program and couldn't figure out how they handle the keyboard hiding.

    Another question: How do I dismiss a "Phone Pad" or "Number Pad" keyboard? The textFieldShouldReturn is ignored since there is no Return or Done key. I can set a "Save" or "Cancel" button somewhere in the navigation bar but I really don't know what those buttons need to do in order to hide this keyboard.

    Thanks in advance,

    Arubinst
     
  2. jerdiver macrumors newbie

    Joined:
    Oct 10, 2008
    #2
    Hi,

    I'm very new to XCode but have you tried to uncheck the "Auto-enable Return Key" text field attribute in Interface Builder ?
     
  3. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #3
    Look at UITextInputTraits.

    resignFirstResponder will hide the keyboard.
     
  4. arubinst thread starter macrumors 6502

    Joined:
    May 26, 2008
    Location:
    Lausanne - Switzerland
    #4
    That did it! Thanks!
     
  5. mozmac macrumors 6502

    mozmac

    Joined:
    Apr 28, 2005
    Location:
    Austin, TX
    #5
    I'm still wondering the answer to the second question. How do you hide the number pad keyboard? Is there a way to add a "Done" button to the bottom left key on the pad, since it is an empty spot?
     

Share This Page