0

I am having some issues with segueing to a new viewcontroller via the show segue. I have been able to use the present segue, but want to use a push/show navigation stack. I am getting this error... "an existing transition or presentation is occurring; the navigation stack will not be updated." Thanks for any help in advance!

// segue to searchVC from homecontroller

    func handleSearch() {
        let vc = SearchVC()
        show(vc, sender: self)

    }

// setup search bar in searchVC class

    func loadPlacesSearchBar() {

        searchController?.searchBar.isHidden = false

        resultsViewController = GMSAutocompleteResultsViewController()
        resultsViewController?.delegate = self

        searchController = UISearchController(searchResultsController: resultsViewController)
        searchController?.searchResultsUpdater = resultsViewController

        // Put the search bar in the navigation bar.
        searchController?.searchBar.sizeToFit()

        navigationItem.titleView = searchController?.searchBar
        searchController?.searchBar.placeholder = "search places"

        // When UISearchController presents the results view, present it in
        // this view controller, not one further up the chain.
        definesPresentationContext = true

        // Prevent the navigation bar from being hidden when searching.
        searchController?.hidesNavigationBarDuringPresentation = false

    }

// extension from SearchVC

extension SearchVC: GMSAutocompleteResultsViewControllerDelegate {

    func resultsController(_ resultsController: GMSAutocompleteResultsViewController,
                           didAutocompleteWith place: GMSPlace) {

        searchController?.isActive = false

        // Do something with the selected place.

        let autocompleteController = GMSAutocompleteViewController()
        autocompleteController.delegate = self as? GMSAutocompleteViewControllerDelegate

        print("Place name: \(place.name)")
        print("Place address: \(String(describing: place.formattedAddress))")
        print("Place attributions: \(String(describing: place.attributions))")

        self.dismiss(animated: true, completion: nil)

        let mapView = MapViewController()
        show(mapView, sender: self)

//        let mapView = MapViewController()
//        let navController = UINavigationController(rootViewController: mapView)
//        present(navController, animated: true, completion: nil)

        DispatchQueue.main.async {
            self.dismiss(animated: true, completion: nil)
        }

    }

    func resultsController(_ resultsController: GMSAutocompleteResultsViewController,
                           didFailAutocompleteWithError error: Error){
        // TODO: handle the error.
        print("Error: ", error.localizedDescription)
    }

    // Turn the network activity indicator on and off again.
    func didRequestAutocompletePredictions(_ viewController: GMSAutocompleteViewController) {
        UIApplication.shared.isNetworkActivityIndicatorVisible = true
    }

    func didUpdateAutocompletePredictions(_ viewController: GMSAutocompleteViewController) {
        UIApplication.shared.isNetworkActivityIndicatorVisible = false
    }
}
user3708224
  • 1,229
  • 4
  • 19
  • 37

1 Answers1

0

The reason you're running into this is that the searchController will be dismissed when you make your selection, and thus, be in the middle of an animation when you're trying to push that new view controller.

A very simple solution that works for me is setting your view controller as a UISearchControllerDelegate, and then calling your segue after the searchController has been dismissed.

    searchController = UISearchController(searchResultsController: resultsViewController)
    searchController?.searchResultsUpdater = resultsViewController
    searchController?.delegate = self //<- add this

Then conform to the Delegate and implement this

extension YourViewController:UISearchControllerDelegate {

func didDismissSearchController(_ searchController: UISearchController) {
   // perform your segue, or push/present your view controller here
}
Priest
  • 242
  • 4
  • 11