Question: Hiding the keyboard when a UITextField is empty

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

  1. arubinst macrumors 6502

    May 26, 2008
    Lausanne - Switzerland

    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:

    - (BOOL)textFieldShouldReturn:(UITextField *)textField {
    	[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,

  2. jerdiver macrumors newbie

    Oct 10, 2008

    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 68040


    Sep 2, 2008
    Look at UITextInputTraits.

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

    May 26, 2008
    Lausanne - Switzerland
    That did it! Thanks!
  5. mozmac macrumors 6502


    Apr 28, 2005
    Austin, TX
    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