Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search Bar Cancel Button is Not Working

My App is having a search bar for searching records from the table view,which is populated by sqlite DB. My problem is that when the view opens the "cancel" button is not enabled and also I cant touch on that, just like a image only.It is there but no action is with that. when we click on that search bar text the cancel button will be changed to "done" it is enabled one. so here is my code

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

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 that

Please check my code and give me the answer. I need to enable the "Cancel" button when the view is loaded and it action will be go back to previous view

I need like this

enter image description here

Or else how can I add a another cancel button on exciting cancel button.so that I can enable that.please give me all the details

like image 584
Ramz Avatar asked May 09 '11 06:05

Ramz


4 Answers

You need to set the UISearchDisplayController to be ACTIVE, like this:

 [mySearchDisplayController setActive:YES animated:YES];

or more simply:

 mySearchDisplayController.active = YES;
like image 99
Rayfleck Avatar answered Nov 02 '22 09:11

Rayfleck


My guess is that Apple made the UISearchBar in a way that the cancel button is disabled if the search text field is empty or not first responder.

This is make sense because you should not use the "Cancel" button to other purpose than actually canceling the search. and since there is no search to cancel - the button is disabled.

If you still want that the button will be active immediately when the view is presented, you can call at viewWillAppear: to [mySearchBar becomeFirstResponder];

This will cause to the keyboard to appear and the button will be enabled. And then if the user hit cancel you can intercept it to go back to the previous view. (I'm not sure if apple will like this behavior).

Sample code:

-(void) viewWillAppear : (BOOL) animated
{
    [super viewWillAppear:animated];
    // Make keyboard pop and enable the "Cancel" button.
    [self.mySearchBar becomeFirstResponder];
}
like image 21
Avi Shukron Avatar answered Nov 02 '22 08:11

Avi Shukron


Here's what I did to always enable the cancel button, even when the search field is not first responder.

I'm calling this method whenever I call resignFirstResponder on the search field

- (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 whether it will pass App Store verification yet, so use it at your own risk. Also, this probably only works if the cancel button is the only button you are using with the search field.

like image 3
Sascha Avatar answered Nov 02 '22 09:11

Sascha


This works to reenable the cancel button as of iOS 8:

private func enableButtonsInSubviews(view: UIView) {
    if let view = view as? UIButton {
        view.enabled = true
    }

    for subview in view.subviews {
        enableButtonsInSubviews(subview)
    }
}
like image 1
Gil Feig Avatar answered Nov 02 '22 09:11

Gil Feig