selectall uitextfield does not always select all
-
11-12-2019 - |
Question
- (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?
Solution
I have found a perfect solution(invoke selectAll in next runloop):
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[textField performSelector:@selector(selectAll:) withObject:textField afterDelay:0.f];
}
OTHER TIPS
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
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow