Simon
Simon

Reputation: 6523

UISearchController: searchResultsController not displaying

I am pretty new to UISearchController so pardon if this is a silly question.

I tried following this question but was not able to get the results I wanted.

In my MapViewController's viewDidLoad I initialize the search controller like so:

searchController = UISearchController(searchResultsController: customViewController)

where customViewController is a subclass UIViewController with some text labels for now (through interface builder). (I declare it like so: let customViewController = MyCustomeViewController()

I tried to follow the above link by hiding and unhiding the searchResultsController with searchController.searchResultsController?.view.isHidden = false in the delegate methods to no luck.

The result is the same: where only the view is dimmed.

Ideally I want it to be something similar to Facebook's app where when you tap the search bar it shows a different view.

I printed the searchResultsController object in the delegate willPresentSearchController and it does indicate it as MyCustomViewController.

Am I missing something here or is this not the right approach?

enter image description here

Upvotes: 4

Views: 1112

Answers (2)

Joshua Wolff
Joshua Wolff

Reputation: 3351

I have found that @Simon's answer is not correct. I am able to use a UIViewController for the searchResultsController.

My issue was that the searchController itself was deallocated from memory because I did not retain it, in this case, as a class-wide instance.

In other words,

Instead of

let searchController = UISearchController(searchResultsController: MySearchResultsController())
...
self.navigationItem.titleView = self.searchController.searchBar
// And add other delegates and stuff

I made a class-wide global and did

private var searchController : UISearchController!

...

self.searchController = UISearchController(searchResultsController: MySearchResultsController())
...

self.navigationItem.titleView = self.searchController.searchBar
// And add other delegates and stuff

Upvotes: 0

Simon
Simon

Reputation: 6523

Turns out that the controller to display needs to be a UITableViewControlleror a subclass of one. I tried a regular UIViewController with a UITableView inside it but no luck.

Upvotes: 7

Related Questions