The button "Cancel search" does not work

My application has a search bar to search for records from a table view that is populated with sqlite DB. My problem is that when I open the view, the Cancel button is not turned on, and I can’t touch it, just like the image itself. It is, but no action is taken with this. when we click on the text of the search bar, the cancel button will be changed to "done", it will be turned on. so here is my code

this is my kind of search bar, see this cancel button. It is not included.

this is my search bar view.see that cancel button.It is not enabled

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar { //[newSearchBar setShowsCancelButton:YES animated:YES]; newSearchBar.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters; NSLog(@"search begin edit") ; //searchString = searchBar.text; //NSLog(@"print did edit searchstring : %@", searchString) ; for(UIView *view in [searchBar subviews]) { //shareItemId =newSearchBar.text; if([view isKindOfClass:[NSClassFromString(@"UINavigationButton") class]]) { [(UIBarItem *)view setTitle:@"Done"]; } } - (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar { NSLog(@"searchBarTextDidEndEditing:"); [searchBar resignFirstResponder]; //[self dismissModalViewControllerAnimated:YES]; } - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar { NSLog(@"searchBarSearchButtonClicked"); searchString = searchBar.text; NSLog(@"search %@", searchBar.text); [newSearchBar setShowsCancelButton:NO animated:YES]; [searchBar resignFirstResponder]; //[self dismissModalViewControllerAnimated:YES]; } - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar { NSLog(@" searchBarCancelButtonClicked"); [searchBar resignFirstResponder]; shareItemName =newSearchBar.text; [self dismissModalViewControllerAnimated:YES]; } - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar { NSLog(@"searchBarShouldBeginEditing"); [newSearchBar setShowsCancelButton:YES animated:YES]; return YES; } 

These are my delegates for this

Please check my code and answer me. I need to enable the Cancel button when loading a view, and this action will revert to the previous view

I like it

enter image description here

Or how can I add another cancel button when the cancel button is activated. So that I can enable this.please, give me all the details

+6
source share
4 answers

I assume that Apple made the UISearchBar in such a way that the cancel button is disabled if the search text field is empty or does not respond first.

This makes sense because you should not use the "Cancel" button for purposes other than actually canceling the search. and since the search is not canceled, the button is disabled.

If you still want the button to turn on immediately when the view is presented, you can call viewWillAppear: before [mySearchBar becomeFirstResponder];

This will bring up the keyboard and the button will be turned on. And then, if the user hits, you can intercept him to return to the previous view. (I'm not sure if Apple will like this behavior).

Code example:

 -(void) viewWillAppear : (BOOL) animated { [super viewWillAppear:animated]; // Make keyboard pop and enable the "Cancel" button. [self.mySearchBar becomeFirstResponder]; } 
+4
source

You need to configure UISearchDisplayController as ACTIVE, for example:

  [mySearchDisplayController setActive:YES animated:YES]; 

or more simply:

  mySearchDisplayController.active = YES; 
+6
source

Here I did to always activate the cancel button, even if the search field is not the first responder.

I call this method when I call resignFirstResponder in the search box

 - (void)enableCancelButton { for (UIView *view in self.searchBar.subviews) { if ([view isKindOfClass:[UIButton class]]) { [(UIButton *)view setEnabled:YES]; } } } 

This works, but I'm not sure if the App Store will still pass the test, so use it at your own risk. Also, this probably only works if the cancel button is the only button that you use with the search field.

+3
source

This works to reactivate the cancel button with iOS 8:

 private func enableButtonsInSubviews(view: UIView) { if let view = view as? UIButton { view.enabled = true } for subview in view.subviews { enableButtonsInSubviews(subview) } } 
0
source

Source: https://habr.com/ru/post/887595/


All Articles