3
votes

I'm using UISPlitViewController in order to show categorized data on the screen. I implemented a search method on master view controller and the flow of data is fine on the iPhone screen, but on the iPad, portrait and landscape, It does work as I want.

I have create a segue (the black arrow points it) to detail controller but it loads data into master view controller.

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "showDetail" {
        if let indexPath = self.tableView.indexPathForSelectedRow {
            let object = categoryID[indexPath.row]
            let controller = (segue.destination as! UINavigationController).topViewController as! DetailTableViewController
            controller.detailItem = object as Int?
            controller.navigationItem.leftBarButtonItem = self.splitViewController?.displayModeButtonItem
            controller.navigationItem.leftItemsSupplementBackButton = true
        }
    //this is the problem!
    }else if segue.identifier == "showSearchDetail" {
        if let indexPath = self.tableView.indexPathForSelectedRow {
            let object = searchResultDrugsID[indexPath.row]
            let controller = (segue.destination as! UINavigationController).topViewController as! DrugDetailsTableViewController
            controller.detailItem = object as Int?
            controller.navigationItem.leftBarButtonItem = self.splitViewController?.displayModeButtonItem
            controller.navigationItem.leftItemsSupplementBackButton = true
        }
    }

}

Here is my storyboard: enter image description here

Here is the normal behavior: enter image description here

And here is where the problem lies, I want to load data in detail view controller when user clicks on search results: enter image description here

1
The master view should be a table that slides to another table view (in the same frame), then from there, go to the detail view?Rob
It does not slide to another table in the master view, it just reload different data when user types in search bar.Maysam
What I am saying is you are showing click in master, go to submenu in detail then click to get to detail page. It should be click in master, slides to submenu in master, click to detail.Rob
well, its default behavior is like what you mentioned, like the first demo, but the second one is a bit different; When user clicks on a row in the master it should [what I want] show data in the detail view.Maysam
Make sure that the segue pointed by the arrow is "Show Detail (eg Replace)".bubuxu

1 Answers

2
votes

Too many Navigation Controllers, too many segues.

From the storyboard screen shot, I can only assume that you may have mixed and matched detail view controllers improperly. Specifically, the one with the black arrow shouldn't be there.

You probably do not need (and shouldn't have) both a "showDetail" and a "showSearchDetail": searching is merely reducing the scope of what's available in the Master view, which is the expected user experience.
An excellent resource on the topic is Candy Search from raywenderlich.com, with an excellent example and tutorial.

Creating a default Master-Detail project from Xcode yields this structure:

5 views master-detail

To add a UISearchController to that, build it programmatically in your master UITableViewController:

let searchController = UISearchController(searchResultsController: nil)

searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
tableView.tableHeaderView = searchController.searchBar
definesPresentationContext = true

and filter the results from the UISearchResultsUpdating delegate:

extension MasterViewController: UISearchResultsUpdating {
  func updateSearchResults(for searchController: UISearchController) {
    filter(searchController.searchBar.text!)
  }
}

func filter(_ searchText: String) {
  // Your filter goes here
  tableView.reloadData()
}

Also, details segues should "Show Details"

Proper segue