我有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的效果" pop"选项依次,但它们不起作用):
更新: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:
更新:self.view.window!.rootViewController?.dismiss(animated: false, completion: nil)
具有相同的效果
我该如何解决这个问题,并转换到MainVC
??