0

I have an alert whose OK button should pop to the root view controller.

Here's the structure of what I'm working with:

[tab view controller] -> [navigation controller] -> [view controller] -> [view controller] -> [navigation controller] -> [view controller] -> [alert]

I would like the OK button on the alert take me to the view controller I've bolded above. When I do the code below, I go back to the view controller I've italicized above, which isn't quite what I want. Any help would be much appreciated! Thanks!

alert.addAction(UIAlertAction(title: OK, style: UIAlertActionStyle.default, handler: { action in

    DispatchQueue.main.async(execute: {
        _ = self.dismiss(animated: true, completion: nil)

    })
}))

I have also tried using the special method for popping to the root view controller, but this has not worked, sadly.

7
  • how do you used this once [view controller] -> [view controller] , the segue type as PUSH or present Commented May 8, 2017 at 5:56
  • In my storyboard, I use Push
    – mlecoz
    Commented May 8, 2017 at 6:00
  • which view Controller is it opening. If you can tell me which ViewController opens from your viewHierarchy graph then I can help you out Commented May 8, 2017 at 6:16
  • it opens the one in italics (slanted letters), after the bold one
    – mlecoz
    Commented May 8, 2017 at 6:19
  • The ViewController is the ViewController in which your naviagtion controller is present. Commented May 8, 2017 at 6:24

3 Answers 3

7

The UINavigationViewController class has a method func popToRootViewController(animated: Bool) -> [UIViewController]? (Documentation)

You can just call this method on the fist navigation view controller. (Note: Therefore you need to have a reference to this navigation view controller or delegates to call this method)

If you use a Storyboard you can use a segue to unwind to the correct view controller. Therefore see this post.

10
  • I have tried using popToRootViewController, but I've had some issues. Based on your answer, it might be because I am calling it on the later navigationViewController. How do I access the first navigation view controller?
    – mlecoz
    Commented May 8, 2017 at 6:01
  • (how do I access it from the very last view controller, that is)
    – mlecoz
    Commented May 8, 2017 at 6:03
  • Yes, you need to call it on the first navigation view controller. And yes, therefore you need to keep a reference. Are you using a Storyboard? Than segues might help you (see here) Otherwise either create delegates along the way or store it somewhere you can access (maybe in the later navigation view controller if you have a subclass?) - Sorry not easy to answer without knowing more of your code :)
    – Nef10
    Commented May 8, 2017 at 6:05
  • I am using a storyboard. I just checked out that link, and I see that it suggests unwinding. I actually tried that already and had the same problem! I did so using this tutorial (andrewcbancroft.com/2015/12/18/…), which has successfully worked for me in the past. Is there anything else that I can tell you about my code that might help?
    – mlecoz
    Commented May 8, 2017 at 6:15
  • What exactly is the problem when using the unwind segue?
    – Nef10
    Commented May 8, 2017 at 6:20
0
let tabVc = (UIApplication.shared.delegate as! AppDelegate).window?.rootViewController as? UITabBarController
tabVc?.selectedViewController?.navigationController?.popToRootViewController(animated: true)
0

You need to navigate the particular index of navigate stack. E.g:- First get the count of view controllers present in navigation stack. Pop to particular index. In you case may be its index is 0:

let arr_controller:[UIViewController] = (self.navigationController?.viewControllers)!
_ = self.navigationController?.popToViewController(arr_controller[0], animated: true)

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.