swift - 从 GMSAutocompleteResultsViewController 选择位置时显示 segue(以编程方式)

标签 swift segue uisearchbar google-places gmsautocomplete

我在通过显示转场转入新的 View Controller 时遇到一些问题。我已经能够使用当前的 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
    }
}

最佳答案

您遇到此问题的原因是,当您做出选择时,searchController 将被关闭,因此,当您尝试推送新的内容时,它会处于动画中间 View Controller 。

对我有用的一个非常简单的解决方案是将您的 View Controller 设置为 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
}

关于swift - 从 GMSAutocompleteResultsViewController 选择位置时显示 segue(以编程方式),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46137091/

相关文章:

ios - 如何运行命令

swift - 如何关闭模态并同时发送数据并执行segue

ios - 在 CollectionView 标题单元格中访问 SearchBar - Swift

ios - Swift 是 UIImage 类实习

swift - CoreMIDI:接收来自 MIDISendSysex 的响应

swift - 关于ARC,下面的代码中Kraken是否强引用了Sucker?

ios - viewDidLoad() 在 prepareForSegue() 完成之前被调用 : Swift

swift - 确定交互式 TableView 标题中的节索引

iOS UITableView 在搜索时不会重新加载数据

ios - 在填充有 .plist 文件的 UITableView 中使用 UISearchBar