6
votes

I have a parent class with tableview and searchbar over it which is a subclass of tableview controller. Delegates for the searchBar and searchdisplaycontroller are set in a seperate class inherited from UISearchdisplaycontroller. The datasource and delegates for tableview and searchbar are handled in this class seperately. The classes are under ARC.

Hence, When a user taps on search, the control transfers from FilesListController (parent)class to this class. Now, When a user taps on cancel button, the searchbar delegate set in this class i.e.

- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar  

is CALLED but DOESN'T serve the purpose of dismissing the full screen searchtableview and return to the parentviewcontroller. However, if I don't write this delegate in the search class, it works properly. I have set the searchbar delegates in xib and on calling:

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar

like this:

self.searchResultsTableView.delegate = self;
self.searchResultsTableView.dataSource = self;
[parentFileViewController.searchDisplayController setDelegate:self];

Where am I going wrong? Thanks in advance.

3
[self.searchDisplayController setActive:NO animated:YES];jussi
hey thanks @jussi .. it works!!! But why doesn't it dismiss by itself? This is like we are forcing the searchviewcontroller to dismiss.tech savvy
because some want to store some Information before dissmissing the Controller. I will post this as an Answer.jussi

3 Answers

13
votes

If you want to dismiss a UISearchBar with a SearchBarController, just use this Code:

[self.searchDisplayController setActive:NO animated:YES];
1
votes

you should implement resign the responder in the delegate function i.e

- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar {
      [searchBar resignFirstResponder];
 }
-1
votes

Memory warnings can appear at any time during the application run time, you must assume a memory warning will happen and the view and disposable objects will have to be recreated.

We are handling such situation by setting to nil our arrays:

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];

if([self isViewLoaded] && self.view.window == nil)
{
    self.view = nil;
    keys = nil;
    names = nil;
    errorDuringNetworkCall = nil;
}
}

And by dismissing the search bar tableview before performing the segue operation:

[self performSegueWithIdentifier:@"navigateToNextScreen" sender:self];
self.searchBar.text = @"";
[self.searchDisplayController setActive:NO animated:YES];

After a Memory warning is received the viewDidLoad method is called again and the arrays are populated, the search bar will continue to be useful.work without issues