13
-(IBAction)settings:(id)sender
{
    [mileagerate resignFirstResponder];

    [mainView bringSubviewToFront:mileageView];
    mainView.hidden=TRUE;

    [UIView beginAnimations:@"Settings" context:nil];
    [UIView setAnimationDuration:1.0];
    [mainView setFrame:CGRectMake(0, 0, 320, 460)];
    [UIView commitAnimations];
    mileagerate.text = [[NSUserDefaults standardUserDefaults] valueForKey:@"savedstring"];

    mileageView.hidden=FALSE;
}

I used resign first responder in various places in my app. But when i click on this button alone, the keyboard does not disappear. If i click on the return key in keyboard, the entered value disappears.

Mehul Mistri
  • 15,037
  • 14
  • 70
  • 94
Sharanya
  • 311
  • 1
  • 2
  • 10

9 Answers9

35

Use below line of code in your method if you don't know which textfield to resign:

Swift

self.view.endEditing(true)//resign current keyboard 

Objective-C

[self.view endEditing:YES]; //resign current keyboard 

EDIT : Note : It will resign keybord from your current view.

Paresh Navadiya
  • 38,095
  • 11
  • 81
  • 132
  • Its work in simulator but not working in device. do you know what is the issues? – Sunil Targe Dec 20 '12 at 15:59
  • 1
    in this issue you will have to debug code and check the flow as case might be another textfield becomes first responder after dissmissing the current textfiled. – Paresh Navadiya Dec 21 '12 at 03:36
  • That wasn't issue... actually I hadn't resign previously becomeFirstResponder, so that I was facing the issue.. So thanks – Sunil Targe Dec 21 '12 at 05:53
  • In my case this didn't work because I was overriding `textFieldShouldEndEditing` and returning `false` in a specific case. Once I changed it to return `true` the keyboard finally went away as expected. – Nick Shelley Mar 15 '17 at 19:37
4

check your .h file if you have put the TextFieldDelegate over there. . Some time if we not declare its delegates function doesn't work properly.

B25Dec
  • 2,301
  • 5
  • 31
  • 54
1

I think when you are setting the text, textField mileagerate is becoming first responder again, so use this statement at last of the method

[mileagerate resignFirstResponder];
Michael Berry
  • 70,193
  • 21
  • 157
  • 216
1

You have to first use a [UIView setAnimationDidStopSelector:@selector(yourMethod)];

Then in the "YourMethod" resign your keyboard.

- (void)yourMethod
{
   [mileagerate resignFirstResponder];
}
Midhun MP
  • 103,496
  • 31
  • 153
  • 200
Sham
  • 475
  • 2
  • 7
1

In Swift 2, you can use the below 3 different types to dismiss keyboard ( assuming there is a textfield and a button

    import UIKit

    class ViewController: UIViewController, UITextFieldDelegate {

        @IBOutlet var userInputField: UITextField!

        override func viewDidLoad() {
            super.viewDidLoad()
            //Important if you are using textfieldDelegate
            self.userInputField.delegate = self
        }


        @IBAction func findAgeBtn(sender: UIButton) {

//1. keyboard to disappear when button clicked
            dimissKeyboard()

        }

//2. keyboard to disappear when touched anywhere on the view

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        self.view.endEditing(true)

    }

//Function for option 1
        func dimissKeyboard(){
            self.view.endEditing(true)
}


//3. keyboard to disappear when clicked on the Return key. Don't forget to set the delegate to self
    func textFieldShouldReturn(textField: UITextField) -> Bool {
                    textField.resignFirstResponder()
                return true
            }
        }
Naishta
  • 11,885
  • 4
  • 72
  • 54
0

Make sure you have connected up the return key to resign first repsonder

On connections tab change connection to "Did End On Exit" and connect to view controller (orange ball) change keyboard return key to DONE.

JSA986
  • 5,870
  • 9
  • 45
  • 91
0

Try this:

if([mileagerate isFirstResponder])
        [mileagerate resignFirstResponder];
Anil
  • 2,430
  • 3
  • 37
  • 55
0

1.Check the UITextFieldDelegate ,

class YourClass : UIViewController,UITextFieldDelegate{

2.Then Override the function "Should End Editing" to return it to Yes

    func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {  
    return true
}
Braham Youssef
  • 479
  • 6
  • 6
0

Swift 4+

I put self.view.endEditing(true) in DispatchQueue and it worked for me.

DispatchQueue.main.async { self.view.endEditing(true) }

Sandeep Maurya
  • 1,979
  • 17
  • 22