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.
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 .
You need to navigate the particular index of navigate stack. Eg:- 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)
let tabVc = (UIApplication.shared.delegate as! AppDelegate).window?.rootViewController as? UITabBarController
tabVc?.selectedViewController?.navigationController?.popToRootViewController(animated: true)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.