0
votes

I have a tabBar controller as parent and I'm pushing a view controller with navigationController. then presenting 2 other view Controllers above to it. its like Tabbar(homeController) -> Child1(push) -> Child 2(presenting) -> child3(presenting). and im dismiss my last childViewController using timer. when its dismissing I want to go back to 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. But its come back to child1 viewContoller. need some help here.

3
what is vc in parent?.present(vc, animated: true, completion: nil)?Giuseppe Lanza
Are you using a storyboard or is everything being setup manually?Upholder Of Truth
@UpholderOfTruth I'm using segueSarath
See my answer on how to handle this.Upholder Of Truth

3 Answers

1
votes

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:

@IBAction func unwindToTop(segue:UIStoryboardSegue) { }

(you can call it what you want as long as you start it '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. The system will do all the back traversal for you.

0
votes

Finally found one other option here.

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

Above code will dismiss all the presented ViewControllers. If you put it in least child(child 3 in my case) it will automattically dismiss my child 2, child 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
            })
        }