1 Reply
      Latest reply on Sep 13, 2019 12:26 PM by Macho Man Randy Savage
      grubinho Level 1 Level 1 (0 points)



        I recently started developing for iOS and I ran into my first problem. I'm trying to provide a searchbar inside a NavigationController scene, the results are shown in a different view because I'm using the searchResultsController method, where another TableView gets called with all results in it. From there you can click on a certain result cell and get details. What I'm trying to figure out is, how you can integrate the results into the main navigation.


        I see a lot of application that seem to use that kind of approach, e.g. facebook. In your news feed you have you a search bar, when you click on it, it opens a tableView (searchResultsController method?), from there you can click on a result and see the detail view. In contrast to my app you still have a navigation bar on top, where you can go back to search result and from there back to the main news feed.


        Obviously I could simply add a button to dismiss my detail view which would bring me back to the main or searchresult view, but I guess there has to be a better way to do it. Or are those apps also just "fake" a navigation bar and use another approach? I want to it be like this:


        main view (with navigation bar and searchbar) -> search result -> details AND then a back button inside details to go details->search result->main view


        Right now I don't get how I put this together, as the tableView with my results is obviously not directly connected to the main view, as I'm using the following method


        let tableViewController = storyboard!.instantiateViewController(withIdentifier: "TableViewController") as! TableViewController
        resultSearchController = UISearchController(searchResultsController: tableViewController)
        resultSearchController?.searchResultsUpdater = tableViewController
        resultSearchController?.hidesNavigationBarDuringPresentation = false
        resultSearchController?.dimsBackgroundDuringPresentation = true
        definesPresentationContext = true


        Currently my storyboard looks like this: https://i.imgur.com/EoTICbp.png

        • Re: Navigation when using a searchResultsController
          Macho Man Randy Savage Level 3 Level 3 (385 points)

          this is kind of annoying to deal with.


          Using a new view controller to display the search results has some benefits. I just wish they would have implemented it a little differently. The search results controller gets shown modally. And if it presents or pushes anything itself, it can throw your navigation UI out of whack.


          If you have a 'show' segue wired up on the search results controller you'll likely get a modal presentation instead of a push. Your going to have to manually relay back to the main view controller the event and push to wherever you need to go on the 'main' navigation stack.


          Also if the search results controller has any calls that present a modal view controllers like so:


          [self presentViewController:someOtherVCNested animated:YES completion:nil];


          And you have a call to dismiss the view controller later like this:


          //Dismiss the someOtherVCNested later.
          [self dismissViewController:someOtherVCNested animated:NO completion:nil];

          You will have orphaned view controllers presented modally. You are going to have to change *all* these methods calls in the search results controller to the following:


            [someOtherVCNested dismissViewControllerAnimated:YES completion:nil];


          This is because UIKIt actually prevents the searchResultsController from presenting the view controller itself, but silently has a view controller  further back in the stack present it. This seems like a silly thing to do.