Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

selectall uitextfield does not always select all

- (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?

like image 690
shiggity Avatar asked Oct 01 '12 02:10

shiggity


2 Answers

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

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [textField performSelector:@selector(selectAll:) withObject:textField afterDelay:0.f];
}
like image 97
flypig Avatar answered Sep 16 '22 20:09

flypig


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
    }
like image 36
Shain Lafazan Avatar answered Sep 17 '22 20:09

Shain Lafazan