9
- (void)textFieldDidBeginEditing:(UITextField *)textField {
    [textField selectAll:self];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;    
}

In the above, the textField selects correctly but when I return from the keyboard and tap the textField for a second time consecutively, it does not select the text. If I do not pick it consecutively or if I deselect the text before returning from the keyboard, the next focus of that textField selects the text correctly.

How can I select the text in the abovementioned case?

tenorsax
  • 21,123
  • 9
  • 60
  • 107
shiggity
  • 531
  • 4
  • 12

2 Answers2

10

I have found a perfect solution(invoke selectAll in next runloop):

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [textField performSelector:@selector(selectAll:) withObject:textField afterDelay:0.f];
}
flypig
  • 1,260
  • 1
  • 18
  • 23
2

I solved this issue using Grand Central Dispatch. You can wrap [textField selectAll:self]; with a dispatch_async call and dispatch_get_main_queue() as a first parameter.

    dispatch_async(dispatch_get_main_queue()){
        // ... code you want to run on the main queue goes here
    }
Shain Lafazan
  • 323
  • 5
  • 6