从 GMSAutocompleteResultsViewController 选择地点时显示 segue(以编程方式)

Posted

技术标签:

【中文标题】从 GMSAutocompleteResultsViewController 选择地点时显示 segue(以编程方式)【英文标题】:show segue(programmatically) when selecting place from GMSAutocompleteResultsViewController 【发布时间】:2017-09-10 03:42:04 【问题描述】:

我在通过 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
    

【问题讨论】:

【参考方案1】:

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

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

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

然后符合Delegate并实现这个

extension YourViewController:UISearchControllerDelegate 

func didDismissSearchController(_ searchController: UISearchController) 
   // perform your segue, or push/present your view controller here

【讨论】:

以上是关于从 GMSAutocompleteResultsViewController 选择地点时显示 segue(以编程方式)的主要内容,如果未能解决你的问题,请参考以下文章

从PRISM开始学WPFMVVMViewModel?

在 python 中,为啥从数组读取比从列表读取慢?

从图库中挑选或从相机捕获的高质量图像

从PRISM开始学WPFMVVMCommand?

从PRISM开始学WPFPrism?

mysql 主-主-从-从