iOS App Development - How to Dismiss Keyboard

Discussion in 'iOS Apps' started by MacDeuterium, Aug 19, 2014.

  1. MacDeuterium macrumors newbie

    Joined:
    Aug 18, 2014
    Location:
    Canberra, Australia
    #1
    Hello everybody,

    I need to know how to dismiss a keyboard when the user taps a UITextField either by:

    1. The user tapping the 'Done' or the 'Return' button in the bottom left hand side of the keyboard, or;
    2. The user tapping outside the keyboard field, and the keyboard is dismissed (preferred method)

    I'm coding in Xcode 5 for iOS 7 so Objective - C is my preferred language, not Swift. :)

    Thanks!

    :apple:
     
  2. vubk, Aug 19, 2014
    Last edited: Aug 19, 2014

    vubk macrumors newbie

    Joined:
    Aug 19, 2014
    #2
    re: iOS App Development - How to Dismiss Keyboard

    For question 2, you can try the below code:

    Change _textField by your name of textField.
     
  3. MacDeuterium thread starter macrumors newbie

    Joined:
    Aug 18, 2014
    Location:
    Canberra, Australia
  4. AxoNeuron macrumors 65816

    AxoNeuron

    Joined:
    Apr 22, 2012
    Location:
    The Left Coast
    #4
    For the first question, it is somewhat more complicated (but still easy) to make the keyboard go away when the user taps the Done button. First, you have to conform your view controller to the UITextFieldDelegate. Then, you have to use the UITextField delegate method called textFieldShouldReturn:

    Code:
    #pragma mark UITextFieldDelegate methods
    -(BOOL)textFieldShouldReturn:(UITextField *)textField
    {
        [self.textField resignFirstResponder];
        return YES;
    }
    
    Inside of the viewDidLoad method of the view controller, make sure to set self.textFieldX.delegate = self.
     

Share This Page