3
[myTextField becomeFirstResponder];
[myTextField resignFirstResonder];

When I do this -(BOOL)textFieldShouldReturn:(UITextField *)textField , it works. But when I use the same code inside -(void)textFieldDidBeginEditing:(UITextField *)textField , it does not work.

I am certain that it is calling textFieldDidBeginEditing. I have an NSLog inside the method and it is being called.

SpaceDust__
  • 4,844
  • 4
  • 43
  • 82
Chris
  • 5,485
  • 15
  • 68
  • 130
  • Why would you want the textField to resign firstResponder status as soon as editing begins? Also, note that the call to `-becomeFirstResponder` is superfluous -- the textField would have to be firstResponder if it got either of these delegate messages. – jlehr Apr 19 '10 at 00:14
  • Because I am trying to make a DatePicker appear in place of the keyboard. – Chris Apr 19 '10 at 00:56
  • I see the date on this comment is super old, but if anybody is looking for this similar behaviour - just assign the date picker to the text field's .inputView property – Mathieson May 05 '17 at 03:39

2 Answers2

7

This is years back, but needing something identical and this answer being at the top of my search results, I ended up playing and came up with this:

[myTextField performSelector:@selector(resignFirstResponder)
                  withObject:nil
                  afterDelay:0
 ];
7

Before resigning firstResponder status, the textField makes a callback to its own -canResignFirstResponder method, which returns NO if the textField is currently in an edit session. You can implement -textFieldShouldBeginEditing: delegate method to return NO if you want to prevent the editing session from beginning.

jlehr
  • 15,557
  • 5
  • 43
  • 45