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.

mlecoz
  • 409
  • 4
  • 17

3 Answers3

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.

Community
  • 1
  • 1
Nef10
  • 926
  • 2
  • 16
  • 26
  • 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 May 08 '17 at 06:01
  • (how do I access it from the very last view controller, that is) – mlecoz May 08 '17 at 06: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](http://stackoverflow.com/questions/38681691/how-to-pop-back-to-a-view-in-a-different-navigation-controller?rq=1)) 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 May 08 '17 at 06: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 (https://www.andrewcbancroft.com/2015/12/18/working-with-unwind-segues-programmatically-in-swift/), 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 May 08 '17 at 06:15
  • What exactly is the problem when using the unwind segue? – Nef10 May 08 '17 at 06:20
  • Oh, IT JUST STARTED WORKING!! I checked over my work and realized I put my IBAction unwind function in the wrong file on accident! It works now, at long last! Thanks for the help @Nef10 – mlecoz May 08 '17 at 06:30
  • No problem, glad to help. – Nef10 May 08 '17 at 06:32
  • Do you happen to know how I might access my tabBarController from my very last view controller? I ask because there are two possible view controllers I'd want to unwind to, depending on which tab I'm on. self.tabBarController is nil in that last view controller. – mlecoz May 08 '17 at 19:13
  • Try the first line of code in Bilals answer - it is not the best way (IMHO) but should work. Cleaner would still be creating appropriate delegates or have references at the right positions – Nef10 May 08 '17 at 19:17
  • I actually did try his/her first line, and it didn't work! Maybe I'll just go the delegate route. Thanks again! – mlecoz May 08 '17 at 19:55
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)
Ahmad F
  • 30,560
  • 17
  • 97
  • 143
Prit_Singh
  • 21
  • 4
0
let tabVc = (UIApplication.shared.delegate as! AppDelegate).window?.rootViewController as? UITabBarController
tabVc?.selectedViewController?.navigationController?.popToRootViewController(animated: true)
Bilal
  • 18,478
  • 8
  • 57
  • 72