Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UISearchBar select all text

Is there any way to select all text in UISearchBar? I tried [searchBar selectALL:], but it throw the signal (unrecognized selector).

I want to allow user to alter previous search text. At the some time, when user just starts typing new request, the old one should be dismissed. The standard way how to achieve it - select all text at the moment when text begin editing.

like image 866
Mike Keskinov Avatar asked Dec 08 '11 17:12

Mike Keskinov


1 Answers

This can be accomplished using the standard UIResponder semantics. No need to dig down into the private view hierarchy of UISearchBar.

[[UIApplication sharedApplication] sendAction:@selector(selectAll:) to:nil from:nil forEvent:nil]

You can call this from anywhere, and the selectAll: selector will run the responder chain to see if any objects respond to it. Assuming your search bar is currently the first responder (if the user is typing in it), it will respond and the result will be all text selected. If not you can make it the first responder by calling becomeFirstResponder on the search bar.

[_mySearchBar becomeFirstResponder]
[[UIApplication sharedApplication] sendAction:@selector(selectAll:) to:nil from:nil forEvent:nil]
like image 192
Ryan Dorshorst Avatar answered Sep 18 '22 20:09

Ryan Dorshorst