14

I have a UIViewController that is a UISearchBarDelegate and a MKMapViewDelegate. The searchBarSearchButtonClicked event works fine, but when testing in iOS 4.2 the searchBarCancelButtonClicked never gets called when hitting the cancel button. In 4.3 everything works fine. I have other views with identical code and it works fine. I have triple checked the method signatures.

Could it be something to do with the MapView, or am I doing something blatantly wrong?

My .h file:

@interface MyViewController : UIViewController <UISearchBarDelegate,MKMapViewDelegate,UIAlertViewDelegate>{
MKMapView *mapMainView;
UISearchBar *sBar;

}

@property (nonatomic, retain) UISearchBar *sBar;
@end

And I create the search bar like so:

sBar = [[[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320.0, 70.0)] autorelease];
sBar.delegate = self;
sBar.showsCancelButton = YES;
[self.view addSubview:sBar];
[sBar becomeFirstResponder];

The method:

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
searchBar.hidden = YES;
}

Does anyone have an idea of why this may be happening?

Deepesh
  • 8,065
  • 3
  • 28
  • 45
user213517
  • 511
  • 2
  • 5
  • 6

4 Answers4

22

I had the exact same problem. Holding the cancel button for a few seconds worked.

The reason for me was that I had implemented UITapGestureRecognizer in the tableview. So this took precedence over the button click or 'x' button click in the search bar.

The solution in my case was to restrict the gesture recognition to only the backgroundview of the tableview. I guess similar thing might be happening in your case. Try to restrict the gesture recognizers to the minimum subview required and the search bar should be outside that view.

  • Thank you. This was exactly my issue. I had a collection view. I restricted the tap gesture to the background view and it worked. Thanks again. – Isuru Mar 09 '14 at 16:35
  • 1
    I don't have any extra gesture recognisers and the cancel button still doesn't trigger its delegate method as it should. Although the tap-and-hold worked for me, I cannot rely on this behaviour. Thank you for the hint anyway :-) +1 – jweyrich May 23 '14 at 20:31
4

Probably your sbar object are releasing, in this case is an autorelease object, Why ?. Try declaring sBar as IBOutlet property. Make the apropiate links in the Interface Builder, remove the alloc as you code it, put in viewDidUnload self.sbar = nil; and releas it in dealloc. in viewDidLoad put this.

sBar.delegate = self;
sBar.showsCancelButton = YES; // this is an option in object inspector
[self.view addSubview:sBar];
[sBar becomeFirstResponder]; //remove this.

Tell me if it works

bolek
  • 41
  • 1
  • Yeah I tried that already, no dice. The thing is, it works on other view controller screens just fine and works in iOS 4.3. Also, like I posted earlier, holding the cancel button for a few seconds causes it to work. – user213517 May 20 '11 at 15:26
1
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == self.searchDisplayController.searchResultsTableView)
    {
        [self.searchDisplayController setActive:NO animated:YES];
        [self.searchDisplayController.searchBar resignFirstResponder];
    }
}
Mazen Kasser
  • 3,559
  • 2
  • 25
  • 35
1

try this:

sBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320.0, 70.0)];

sBar.delegate = self;

sBar.showsCancelButton = YES;

[self.view addSubview:sBar];

and try to put release in dealloc

Lloyd Powell
  • 18,270
  • 17
  • 87
  • 123
Kumaran
  • 687
  • 6
  • 14