简体   繁体   English

如果您推送并显示多个viewController,如何移动到parentViewController?

[英]How to move to parentViewController if you push and present multiple viewControllers?

I have a tabBar controller as parent and I'm pushing a view controller with navigationController. 我有一个tabBar控制器作为父级,并且正在使用navigationController推送一个视图控制器。 then presenting 2 other view Controllers above to it. 然后在其上方显示其他2个视图控制器。 its like Tabbar(homeController) -> Child1(push) -> Child 2(presenting) -> child3(presenting). 就像Tabbar(homeController)-> Child1(push)-> Child 2(present)-> child3(present)。 and im dismiss my last childViewController using timer. 并且即时通讯使用计时器关闭了我的最后一个childViewController。 when its dismissing I want to go back to tabBarController(home). 当它关闭时,我想回到tabBarController(home)。

let parent = self.presentingViewController
self.dismiss(animated: false, completion: {
    parent?.present(vc, animated: true, completion: nil)
})

I put this above code in my 2nd Child ViewController. 我将以上代码放在第二个子ViewController中。 But its come back to child1 viewContoller. 但是它又回到了child1 viewContoller。 need some help here. 在这里需要一些帮助。

If you are using a storyboard then first setup an unwind segue on the view controller you want to return to (so that tabBarController in this case) like this: 如果您正在使用情节提要,则首先在要返回的视图控制器上设置展开序列(在这种情况下为tabBarController),如下所示:

@IBAction func unwindToTop(segue:UIStoryboardSegue) { }

(you can call it what you want as long as you start it 'unwindTo') (只要将其启动为“ unwindTo”,就可以随意命名)

Then in interface builder create an unwind segue (either direct from an object like a button or more generic from the view controller itself) to the exit of the view controller and select the unwind segue that should appear in the list. 然后,在界面构建器中创建一个展开序列(直接从按钮之类的对象开始,或者从视图控制器本身更通用)到视图控制器的出口,然后选择应该显示在列表中的展开序列。

Then when this segue is performed, either through a storyboard item or manually in code, you will be returned to the view controller that defined the unwind segue. 然后,当通过情节提要项或手动在代码中执行此segue时,您将返回到定义展开segue的视图控制器。 The system will do all the back traversal for you. 系统将为您完成所有反向遍历。

Finally found one other option here. 终于在这里找到了另一个选择。

self.view.window!.rootViewController?.dismissViewControllerAnimated(false, completion: nil)

Above code will dismiss all the presented ViewControllers. 上面的代码将关闭所有显示的ViewController。 If you put it in least child(child 3 in my case) it will automattically dismiss my child 2, child 3. 如果您将它放到至少一个孩子(本例中为孩子3)中,它将自动解散我的孩子2,孩子3。

let parentVC = self.presentingViewController

        if let tabParent = parentVC as? UITabBarController, let firstNav = tabParent.viewControllers![0] as? UINavigationController {
            //present child 2
            self.present(vc, animated: true, completion: {
                firstNav.popToRootViewController(animated: false)// pop to my rootVC
            })
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM