0

我在通过 show segue 连接到新的视图控制器时遇到了一些问题。我已经能够使用当前的 segue,但想使用推送/显示导航堆栈。我收到此错误...“正在发生现有转换或演示;导航堆栈将不会更新。” 提前感谢您的帮助!

// 从 homecontroller 转到 searchVC

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

    }

// 在 searchVC 类中设置搜索栏

    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

    }

// 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
    }
}
4

1 回答 1

0

您遇到此问题的原因是,searchController当您进行选择时,将被取消,因此,当您尝试推送该新视图控制器时,它处于动画中间。

一个对我有用的非常简单的解决方案是将您的视图控制器设置为 a UISearchControllerDelegate,然后在关闭后调用您的 segue searchController

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

然后符合委托并实现这个

extension YourViewController:UISearchControllerDelegate {

func didDismissSearchController(_ searchController: UISearchController) {
   // perform your segue, or push/present your view controller here
}
于 2018-06-07T07:41:36.273 回答