OGeek|极客世界-中国程序员成长平台

标题: ios - 关闭 View Controller 不会返回到前一个 View Controller [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 18:47
标题: ios - 关闭 View Controller 不会返回到前一个 View Controller

根据我的应用程序的需要,在推送另一个带有动画的 View Controller 后,我正在展示一个 View Controller 。代码如下:

    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)
  }

在呈现的 View Controller 中,我分配了一个侧边菜单 Controller ,因为我需要在向右滑动时显示侧边菜单:

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()
  }

现在我在这个 View Controller 中有一个自定义的后退按钮。单击它时,我需要关闭 View Controller 并显示 View Controller 的显示位置。名称为“HomeSearchViewController”

为了解除我在按钮操作中写了以下代码:

    func navigateToPreviousController() {
      self.dismiss(animated: true, completion: nil)
  }

但这并没有解除 View Controller 。我可以知道是什么问题吗?



Best Answer-推荐答案


为了关闭 View Controller ,您的 View Controller 应嵌入导航 Controller 中。

例如:

let controller =storyboard.instantiateViewController(withIdentifier: "storyboardidentifier") as! yourviewcontroller        
    let navController = UINavigationController(rootViewController: controller)
    present(navController, animated: true, completion: nil) 

然后在呈现的 View Controller 上,如果您使用以下代码,它将关闭 View Controller 。

func dismissButtonPressed(){
   self.dismiss(animated: true, completion: nil)
}

干杯!!

关于ios - 关闭 View Controller 不会返回到前一个 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46432773/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) Powered by Discuz! X3.4