我有Navigation Controller
2 个 Viewcontrollers:MainVC
(root)和SearchVC
.
首先,我使用从MainVC
(“+”导航项)到的转换功能SearchVC
:
@objc func goToSearchVC() {
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let searchVC = storyBoard.instantiateViewController(withIdentifier: "SearchVC") as! SearchVC
//Hide system navigation controller back button and add custom close button
DispatchQueue.main.async {
searchVC.navigationItem.hidesBackButton = true
searchVC.navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "closeBtn_20"), style: .plain, target: self, action: #selector(self.closeBtnTapped))
searchVC.navigationItem.title = ""
}
//It's constructed function for custom transition type, in this case transition from top, it's works well
animatedTransition(navi: self.navigationController!,transitionType: kCATransitionFromTop, duration: 0.5)
//push to SearchVC
self.navigationController?.pushViewController(searchVC, animated: false)
}
//close button function for SearchVC navibar
@objc public func closeBtnTapped() {
animatedTransition(navi: self.navigationController!, transitionType: kCATransitionFromBottom, duration: 0.5)
navigationController?.popViewController(animated: false)
let mainNaviBar = self.navigationController!.navigationBar
mainNaviBar.isHidden = true
}
这个“+”推动过渡和关闭按钮“x”弹出过渡效果很好:
在SearchVC
我GMSAutocompleteResultsViewController
用于搜索地点并尝试弹出MainVC
使用goToVc()
功能时(我依次检查了所有 3 个“弹出”选项的性能,但它们不起作用):
更新:self.view.window!.rootViewController?.dismiss(animated: false, completion: nil)
也不起作用
class SearchVC: UIViewController, CLLocationManagerDelegate {
var resultsViewController: GMSAutocompleteResultsViewController?
var searchController: UISearchController?
var resultView: UITextView?
override func viewDidLoad() {
super.viewDidLoad()
//Make navigation bar visible
let mainNaviBar = self.navigationController?.navigationBar
mainNaviBar?.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
// 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
}//viewDidLoad()
//function for segue to existing VC, MainVC
func goToVc() {
//It's constructed function for custom transition type, in this case transition from top, it's works well
animatedTransition(navi:self.navigationController!,transitionType: kCATransitionFromTop, duration: 0.5)
//!!!I checked the performance of all these "pop" options in turn, but they do not work
//Option 1, popToRootViewController (MainVC) - Doesn't work
self.navigationController?.popToRootViewController(animated: false)
//Option 2, popViewController(animated: false) - Doesn't work
self.navigationController?.popViewController(animated: false)
//Option 3, popToViewController - Doesn't work
self.navigationController?.popToViewController(MainVC(), animated: false)
}
}//class
Extension:
// Handle the user's selection.
extension SearchVC: GMSAutocompleteResultsViewControllerDelegate {
func resultsController(_ resultsController: GMSAutocompleteResultsViewController,
didAutocompleteWith place: GMSPlace) {
searchController?.isActive = false
// Do something with the selected place.
print("Place name: \(place.name)")
print("Place address: \(place.formattedAddress)")
print("Place attributions: \(place.attributions)")
print("Place coordinats:\(place.coordinate)")
//segue to MainVC
goToVc()
}
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
}
}
在所有三个“弹出到 MainVC”的情况下都过渡到MainVC
不发生,并且弹出到当前的 VC:
upd :self.view.window!.rootViewController?.dismiss(animated: false, completion: nil)
有同样的效果
我该如何解决这个问题,并过渡到MainVC
??