-
-
Save pruinis/5d99f947f80af83407cd to your computer and use it in GitHub Desktop.
Detecting when clear is clicked in UISearchBar (X button)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| - (void)viewDidLoad { | |
| //find the UITextField view within searchBar (outlet to UISearchBar) | |
| //and assign self as delegate | |
| for (UIView *view in searchBar.subviews){ | |
| if ([view isKindOfClass: [UITextField class]]) { | |
| UITextField *tf = (UITextField *)view; | |
| tf.delegate = self; | |
| break; | |
| } | |
| } | |
| } | |
| - (void)searchBarCancelButtonClicked:(UISearchBar *) aSearchBar { | |
| [aSearchBar resignFirstResponder]; | |
| } | |
| - (BOOL)textFieldShouldClear:(UITextField *)textField { | |
| //if we only try and resignFirstResponder on textField or searchBar, | |
| //the keyboard will not dissapear (at least not on iPad)! | |
| [self performSelector:@selector(searchBarCancelButtonClicked:) withObject:self.searchBar afterDelay: 0.1]; | |
| return YES; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment