Segue to detail view controller on landscape mode

670 views Asked by At

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

There are 1 answers

4
SwiftArchitect On BEST ANSWER

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