Can i pop to Specific ViewController?
Solution 1
By Writing the First Line you get the Indexes of all View Controllers and from second Line You will reach up to your Destination.
NSArray *array = [self.navigationController viewControllers];
[self.navigationController popToViewController:[array objectAtIndex:2] animated:YES];
Solution 2
A safer approach:
- (void)turnBackToAnOldViewController{
for (UIViewController *controller in self.navigationController.viewControllers) {
//Do not forget to import AnOldViewController.h
if ([controller isKindOfClass:[AnOldViewController class]]) {
[self.navigationController popToViewController:controller
animated:YES];
return;
}
}
}
Solution 3
Swifty way:
let dashboardVC = navigationController!.viewControllers.filter { $0 is YourViewController }.first!
navigationController!.popToViewController(dashboardVC, animated: true)
Solution 4
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)
}
Solution 5
- (void) RetunToSpecificViewController{
for (UIViewController *controller in self.navigationController.viewControllers)
{
if ([controller isKindOfClass:[AnOldViewController class]])
{
//Do not forget to import AnOldViewController.h
[self.navigationController popToViewController:controller
animated:YES];
break;
}
}
}
Ankit Vyas
I am a Computer Engineer and Working as an iphone application Developer.
Updated on July 09, 2022Comments
-
Ankit Vyas almost 2 years
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 Third ViewController to First ViewController.I am performing this task using the below code but my application crashed.
Please any body give me some proper guidelines. I can't use pop to rootViewController because it's different viewController. Thanks in advance...
In Third ViewControler i have written this:
FirstViewCtr *x=[[FirstViewCtr alloc] initWithNibName:@"FirstViewCtr" bundle:nil]; [self.navigationController popToViewController:x animated:NO];
-
Ankit Vyas almost 14 yearsI have RootViewController then it Pushed into FirstViewController then after SecondViewController then after ThirdViewController while i reach upto ThirdViewController i required pop Directly to the FirstViewController.
-
Pavan over 11 yearsthats amazing. +1 .. brilliant. im using this in my app right now.
-
JohnK almost 11 yearsSafer than a native method? How so?
-
Yunus Nedim Mehel almost 11 yearsThe native method is the message popToViewController:animated, not the way you are providing the parameters to the message. I believe this solution is safer because when you say [array objectAtIndex:2] , you automatically assume that the number of VC's is bigger than 2. If thats not the case, the program will crash, with my solution it will stand still and wont crash if there is no instance of the required VC.
-
Admin almost 10 yearsi think that your aproach its really better than the accepted solution @YunusNedimMehel. Whatever, and totally offtopic, i think break sentence is not needed there
-
Yunus Nedim Mehel almost 10 years@oPi It is actually relevant, lets discuss that. I have not tested it, but if the for loop does not terminate after popping, it might cause a crash. What if there is another instance of AnOldViewController on the stack?
-
Admin almost 10 years@YunusNedimMehel ive not tested it too, but i think that, when popviewcontroller is called, the current view controller is deallocated (and i think that main thread operations should be stopped)
-
Yunus Nedim Mehel almost 10 years@oPi It is still risky though, because the top viewController will be deallocated after popping animation is completed (0.3s I guess?). So it's better to avoid that
-
Bob Arezina over 9 yearsOr, for the one liner cheat people: [self.navigationController popToViewController:[[self.navigationController viewControllers]objectAtIndex:2] animated:YES];
-
Kanan Vora over 9 yearsYeah, this is the better solution then the accepted answer... it is dynamic where in the accepted answer contains static code...
-
Gajendra Rawat about 8 yearslet suppose AnOldViewController is not there in navigation stack even you want to pop into AnOldViewController. How you will implement this?
-
Yunus Nedim Mehel about 8 years@morroko If it's not on the stack, you cannot pop into it. You can either push it, or manually insert it inside the viewControllers stack (it's a public, readwrite NSArray property)