- (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?
I have found a perfect solution(invoke selectAll in next runloop):
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[textField performSelector:@selector(selectAll:) withObject:textField afterDelay:0.f];
}
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With