解除视图控制器不会返回到上一个视图控制器

时间:2017-09-26 17:44:20

标签: ios swift swift3 uiviewcontroller

我在推送另一个带有动画的视图控制器后呈现视图控制器,因为我的应用程序需要它。代码如下:

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

但这不是解雇视图控制器。我可以知道这是什么问题吗?

2 个答案:

答案 0 :(得分:0)

修改:我的第一个回答只解决了部分问题。

进一步了解您的代码...

您似乎 HomeSearchViewController推送到navigationController堆栈,然后<{1>}来自SearchRideViewController HomeSearchViewController }。

...然而

在:

HomeSearchViewController viewDidLoad()

您创建

的实例
SideBarViewController (leftViewController)

并创建

的实例
SlideMenuTrackerController (slideMenuController)

并指定

self as the .mainViewController and
leftViewController as the .leftMenuViewController
of slideMenuController.

如果这不够混淆,你也可以:

AppDelegate.sharedInstance().window?.rootViewController = slideMenuController

替换 HomeSearchViewController ...您刚刚推到navigationController堆栈!

所以,您的视图层次结构现在(或者,至少,如果我关注您的代码,似乎是这样):

Window
    slideMenuController (instance of SlideMenuTrackerController)
        +- searchViewController (instance of SearchRideViewController)
        +- leftViewController (instance of SideBarViewController)

为了回到&#34;你来自哪里,你需要重新创建你想要的任何VC,并(再次)替换AppDelegate.sharedInstance().window?.rootViewController

如果没有看到整个项目,我就不确定你应该怎么做。

[原始答案]

在这里:

func pushViewController() {
    // ... the other lines
    self.navigationController?.pushViewController(viewcontroller!, animated: false)
    viewcontroller?.present(searchViewController!, animated: true, completion: nil)

}

推送 viewcontroller以及来自 .present(searchViewController!...的{​​{1}} (开始时有点奇怪,但是无论)...

然后在这里:

viewcontroller

func navigateToPreviousController() { self.dismiss(animated: true, completion: nil) } 一个视图控制器,但你永远不会.dismiss回到导航控制器的堆栈上。

答案 1 :(得分:0)

为了解除视图控制器,您的视图控制器应该嵌入在导航控制器中。

例如:

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

干杯!!