I am using navigation based application. I push First ViewController to Second ViewController and from Second ViewController to Third ViewController. Now I want to pop from
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];
Your code creates a new instance of a view that has never been pushed onto the stack, then tries to pop back to that controller.
If you are popping back to the root view controller, you can uses popToRootViewControllerAnimated:
If you are popping back a known distance you can call popViewControllerAnimated:
more than once. In your example, that would be 2 controllers so to calls. You could do the same thing by looking in viewControllers
for the controller 2 from the end and popping to it.
The above suggestions are quick fixes. One best practice scenario would be to pass the controller you want to return to along to each successive controller you push. First passes itself to second, second passes that reference to third, third pops to the passed reference, which is first.
In effect, you are creating a temporary root controller. You could subclass UINavigationController
and add a temporaryRoot
property and a popToTemporaryRootViewControllerAnimated:
method that would pop to your temporary root and clear it. When first pushes seconds, it would also set itself as the temporary root so that every controller in the stack does not have to pass a reference around. You would have to add some extra checks to unsure you never pop past the temporaryRoot without clearing it.
Implemented & Tested in Swift 3.0
Below is Method which can useful for Navigate to any specific View Controller :
func poptoSpecificVC(viewController : Swift.AnyClass){
let viewControllers: [UIViewController] = self.navigationController!.viewControllers
for aViewController in viewControllers {
if aViewController.isKind(of: viewController) {
self.navigationController!.popToViewController(aViewController, animated: true)
break;
}
}
}
Usage :
self.poptoSpecificVC(viewController: createIntervalVC.self)
Swift 4 version
if let viewController = navigationController?.viewControllers.first(where: {$0 is YourViewController}) {
navigationController?.popToViewController(viewController, animated: false)
}
You may specify another filter on .viewControllers.first
as per your need e.g lets say if you have same kind
of view controllers residing in the navigation controller then you may specify an additional check like below
if let viewController = navigationController?.viewControllers.first(where: {
if let current = $0 as? YourViewController {
return current.someProperty == "SOME VALUE"
}
return false } ) {
navigationController?.popToViewController(viewController, animated: false)
}
Swifty way:
let dashboardVC = navigationController!.viewControllers.filter { $0 is YourViewController }.first!
navigationController!.popToViewController(dashboardVC, animated: true)
I think that .filter({...}).first
is a little bit slower than .first(where: {...})
.
Also this could be written more precisely to address only UIViewControllers.
extension UINavigationController {
func popToController<T: UIViewController>(_ type: T.Type, animated: Bool) {
if let vc = viewControllers.first(where: { $0 is T }) {
popToViewController(vc, animated: animated)
}
}
func popToControllerOrToRootControllerIfNotInTheStack<T: UIViewController>(_ type: T.Type, animated: Bool) {
if let vc = viewControllers.first(where: { $0 is T }) {
popToViewController(vc, animated: animated)
} else {
popToRootViewController(animated: animated)
}
}
}