我遇到了一些通过show segue转换到新的viewcontroller的问题。我已经能够使用当前的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
}
}
答案 0 :(得分:0)
您遇到此问题的原因是,当您进行选择时,searchController
将被解除,因此,当您尝试推送新的视图控制器时,将处于动画的中间位置。
一个非常简单的解决方案适用于我将视图控制器设置为UISearchControllerDelegate
,然后在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
}