在我的应用程序需要使用动画推送另一个视图控制器之后,我将展示一个视图控制器。代码如下所示:
func pushViewController() {
let viewcontroller = UIStoryboard(name: "Home", bundle: nil).instantiateViewController(withIdentifier: "homeSearchIndentifier") as? HomeSearchViewController
let searchViewController = UIStoryboard(name: "Home", bundle: nil).instantiateViewController(withIdentifier: "searchRideIdentifier") as? SearchRideViewController
searchViewController?.parentView = .homeLandingPage
let duration = 0.4
let transition: CATransition = CATransition()
transition.duration = duration
transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
transition.type = kCATransitionPush
transition.subtype = kCATransitionFromBottom
self.navigationController!.view.layer.add(transition, forKey: kCATransition)
self.navigationController?.pushViewController(viewcontroller!, animated: false)
viewcontroller?.present(searchViewController!, animated: true, completion: nil)
}
在显示的视图控制器中,我要分配一个侧边菜单控制器,因为我需要在向右滑动时显示侧边菜单:
override func viewDidLoad() {
super.viewDidLoad()
self.addSearchListScreen()
self.locationManager.delegate = self
yourLocation.layer.sublayerTransform = CATransform3DMakeTranslation(7, 0, 0)
let leftViewController = UIStoryboard(name: "SideBar", bundle: nil).instantiateViewController(withIdentifier: "sideBar") as? SideBarViewController
let slideMenuController = SlideMenuTrackerController(mainViewController: self, leftMenuViewController: leftViewController!)
AppDelegate.sharedInstance().window?.rootViewController = slideMenuController
leftViewController?.mainViewController = self
slideMenuController.automaticallyAdjustsScrollViewInsets = true
AppDelegate.sharedInstance().window?.backgroundColor = UIColor(red: 236.0, green: 238.0, blue: 241.0, alpha: 1.0)
AppDelegate.sharedInstance().window?.makeKeyAndVisible()
}
现在,我在此视图控制器中有一个自定义的后退按钮。单击它时,我需要关闭视图控制器,并从显示它的位置显示视图控制器。名称为“HomeSearchViewController”
为了解雇我在按钮操作中编写了以下代码:
func navigateToPreviousController() {
self.dismiss(animated: true, completion: nil)
}
但这并不能消除视图控制器。我可以知道是什么问题吗?
最佳答案
为了关闭视图控制器,您的视图控制器应嵌入导航控制器中。
例如:
let controller =storyboard.instantiateViewController(withIdentifier: "storyboardidentifier") as! yourviewcontroller
let navController = UINavigationController(rootViewController: controller)
present(navController, animated: true, completion: nil)
然后在显示的视图控制器上,如果您使用以下代码,它将关闭视图控制器。
func dismissButtonPressed(){
self.dismiss(animated: true, completion: nil)
}
干杯!!
关于ios - 取消 View Controller 不会返回到先前的 View Controller ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46432773/