How to use UISearchDisplayController from a contro

2019-04-07 08:53发布

问题:

I have an UITabBar controller managing several controllers (using SDK 3.0). One of these is a tableView controller and I need to provide a search capability using the UISearchDisplayController. All of my code is based on Apple TableSearch example. However, when clicking on the tab, the tableView controller appears showing its related content, but no searchBar appears. I have checked the xib in IB to make sure that all of the outlets are properly set, but no matter what I try self.searchDisplayController is always nil and the search bar does not appear.

In practice I have replicated MainView.xib from the TableSearch example and set the file's owner class to the correct controller class for the tab. The outlets are sets as in the example MainView.xib. Am i missing any important step or doing something wrong?

Thank you in advance.

回答1:

I had the same issue and happened to stumble upon this solution ...

If you have your table view controller (eg. UISearchDisplayController) nested in a Tab Bar or Navigation controller using Interface Builder, you need to set the "Nib Name" in the "Attributes Inspector" window.

The Nib name will be the one that holds the table view and has the controller (eg. UISearchDisplayController) as the File's Owner.



回答2:

I too have this issue :( Is the search bar getting hidden behind the tableview?

@unforgiven, did you try this...?

searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 320, 40)]; 
[self.tableView setTableHeaderView: searchBar];

This manually creates a searchbar and it works. But I'm making some stupid mistake in IB that the SearchBar doesn't show up even though my connections are perfect. :-(

Do update this post if you get the answer...



回答3:

Ok, I have found how to solve it. In my case, the problem was due to the fact that I was using a controller embedded within the UITabBarController as one of its managed tabs (i.e. as a child).

Removing the controller from the UITabBarController, then adding an UINavigationController to the UITabBarController instead, and finally putting my controller as a child of the UINavigationController solved completely the issue.

I do not understand why this is so (there is no related information in the documentation, as often happens); however, it now works like a charm. With kind regards.



回答4:

I had a similar issue

To solve I had to do one additional step to unforgivens answer In my main nib

1) create a UITabController 2) Then I dragged out a UINavigational Controller into the tab controller 3) Then dragged out a UITableViewController into the NavigationalController as a child 4) Then changed (3) class to be my MyTableWithSearchBarViewController in the inspector - check if the nib name is correct and change this if necessary in the inspector as well 5) I then had to delete the tableView which is automatically created by IB in step (3). Only then would the search bar show correctly...

If in step 3 I dragged out a different controller onto the stage or left the tableView there it would only ever display the table and not the search bar

weird



回答5:

Tomtrapeze has the right answer if your nib file contains the UITableViewController. But, if you're loading the UITableViewController in code -- such as pushing it on the stack of a UINavigationController -- the solution is a little different.

When initializing the UITableViewController or subclass, you need to use the -initWithNibName:bundle: form of initializer and specify the name of your nib file. Alternately, you could use the standard -initWithStyle: initializer and manually set the nibName property before the view is loaded.

If the nibName property is not set when the view gets loaded, UITableViewController will not use the normal UIViewController nib lookup logic. It will just load a standard UITableView instead.



回答6:

I recently learned that I could load NIB files with [[MyViewController alloc] initWithNibName:nil bundle:nil]; when the NIB file has the same name as the class (i.e. MyViewController.xib). It turns out that initializing this way causes the 'UISearchBarandUISearchBarDisplayController` to not display. When I initialized the view controller by actually typing out the name of the class, my search bar showed up correctly. I initially thought it had something to do with how I was presenting the view controller but was glad that it wasn't.

PGWSearchViewController *searchVC = [[PGWSearchViewController alloc] initWithNibName:@"PGWSearchViewController" bundle:nil];

searchVC.modalPresentationStyle = UIModalPresentationFullScreen;
searchVC.modalTransitionStyle = UIModalTransitionStyleCoverVertical;

[self presentViewController:searchVC animated:YES completion:^{ }];

This is using iOS SDK 5.1 and XCode 4.3.1.



回答7:

Another possible fix that works for me is to init the UITableViewController with initWithNibName:bundle:

SearchEntryTableViewController* searchEntryTableViewController = [[SearchEntryTableViewController alloc]initWithNibName:@"SearchEntryTableViewController" bundle:nil];

To nest the UITableViewController in a UINavigationController before put it in a UITabBarController doesn't fix the issue for me...