0
votes

So I have the following setup.

InitialViewController(VC) -> NavigationController -> ViewController1(VC1) contains a UISearchController, displays results in a new controller ViewController2(VC2) -> Segue from a cell click on VC2 -> Launch ViewController3 (VC3)

I need to dismiss VC3 and come back to VC1. However, neither VC2 nor VC3 are part of the navigation controller stack. I have pretty much tried all the suggested alternatives but of no avail.

How do I push the UISearchcontroller and the results on to the navigation stack ?

I am instantiating my UISearchController in VC1 like this:

    searchController = UISearchController(searchResultsController: 
    searchResultsController)
    searchController!.searchResultsUpdater = searchResultsController
    searchController?.delegate = self
    searchController!.obscuresBackgroundDuringPresentation = true
    searchController!.searchBar.placeholder = "Search stocks"
    searchController?.hidesNavigationBarDuringPresentation = false
    definesPresentationContext = true
    searchController!.searchBar.delegate = searchResultsController
    //navigationItem.searchController = searchController
    navigationItem.hidesSearchBarWhenScrolling = false
    navigationItem.titleView = searchController?.searchBar
    definesPresentationContext = true

And my searchResultsController:

extension WatchListTableViewController: UISearchResultsUpdating, UISearchBarDelegate {
    // MARK: - UISearchResultsUpdating Delegate
    func updateSearchResults(for searchController: UISearchController) {
        // TODO
        filterContentForSearchText(searchController.searchBar.text!)
    }
}
class WatchListTableViewController: UITableViewController {
    var stocks =  [String]()
    var filteredStocks = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()
        stocks = ["Apple", "Google", "Microsoft", "Tesla"]
        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem
    }
1
When you reach vc3 how does your window viewcontroller stack looks ? - Gihan
Can't access the viewcontroller stack, navigationController is nil inside VC3 - Pradeep Banavara
try window.rootViewController. and see - Gihan
oh wow it is VC and not VC1. So Let me try making VC a part of the navigation stack. Would that work ? - Pradeep Banavara
And also check whats in window.rootViewController.childviewcontrollers, I can include a detailed answer for your use case bit later. Its 2am and gotta sleep - Gihan

1 Answers

0
votes

Solved this in the following way:

Removed the Storyboard Segue from VC2 to VC3.

In VC2 - pushed VC3 on to the navigation stack using:

self.presentingViewController?.navigationController?.pushViewController(vc, animated:true)