Question

Use to, this type of syntax was needed in the "Did End On Exit" action of a UITextField:

[self.myTextField resignFirstResponder];

At least in the simulator of Xcocde 5, that code seems to be no longer needed. The keyboard will hide when you press the return button and there is no associated action attached. Is that correct or is this simulator only behavior?

I guess this code is still needed for iOS6?

Was it helpful?

Solution

Actually, the premise of your question is wrong. Nothing has changed, except that you've discovered the truth. It was never necessary to say resignFirstResponder if a text field targeted its "did end on exit" action: the mere fact of the targeting caused (and still causes) the Return key to perform dismissal automatically.

In fact, the "did end on exit" action doesn't actually have to do anything; no corresponding method need be implemented. And the target need not be a real object; it can be nil. In other words, this code will endow any text field with the "magical" property that it automatically dismisses the keyboard when the Return key is pressed:

[textField addTarget:nil action:@selector(dummy:)
    forControlEvents:UIControlEventEditingDidEndOnExit];

That works (and has worked since as early as I've been using iOS, i.e. iOS 3) even though the target is nil and the action method is never implemented anywhere (there is no dummy: method in the app). It always surprises me that this important technique is not more widely known.

OTHER TIPS

You still need it. To close a keyboard I make an action called "close keyboard" and connect that to my text field or text view. Here is the code...

-(IBAction)closeKeyboard {

[textfield1 resignFirstResponder];

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top