0

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.

4
  • what is vc in parent?.present(vc, animated: true, completion: nil)? Commented Jan 10, 2018 at 15:46
  • Are you using a storyboard or is everything being setup manually? Commented Jan 10, 2018 at 16:21
  • @UpholderOfTruth I'm using segue Commented Jan 11, 2018 at 8:04
  • See my answer on how to handle this. Commented Jan 11, 2018 at 8:05

3 Answers 3

0

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

-1

You should check the popToRootViewController method.

https://developer.apple.com/documentation/uikit/uinavigationcontroller/1621855-poptorootviewcontroller

4 Comments

The described flow is chain of modals, not pushed view controllers.
hmm... "I'm pushing a view controller with navigationController. then presenting 2 other view Controllers above to it"
exactly. he has a tabbarcontroller that presents modally a navigation view controller containing child 1. push to child 2 that presents modally child 3 that presents modally child 4. he wants to go back to tabbarcontroller. popToRootViewController is useless.
"its like Tabbar(homeController) -> Child1(push) -> Child 2(presenting) -> child3(presenting)"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.