Reputation: 2811
I want to make a segue that makes navigation controller poptoroot and then make a new controller pushed into the navigation controller . e.g Now the controller hierarchy is
navigationController->rootcontroller->controllerA ->controllerB
and i want to make a segue perfome in controller B,and the segue will make the hierarchy to
navigationController->rootcontroller-> controllerC
is this possible?
Upvotes: 0
Views: 893
Reputation: 1
This code work for me :
self.performSegue(withIdentifier: "segue_id", sender: self)
var navigationArray = self.navigationController?.viewControllers
var i = navigationArray!.count - 2
while i > 0 {
navigationArray!.remove(at: i)
i = i - 1
}
self.navigationController?.viewControllers = navigationArray!
Upvotes: 0
Reputation: 1690
You want to set segue doing pop to root. This can be achieved using Unwind Segues
. For more detail regarding this you can follow this Apple
doc.
For pushing to new VC, You can simply push in storyboard.
Upvotes: 2
Reputation: 78
You can use for loop to pop till root or where you want and use same method to push forward:
// For Pop
NSArray *viewControllers = [[self navigationController] viewControllers];
for( int i=0;i<[viewControllers count];i++){
id obj=[viewControllers objectAtIndex:i];
if([obj isKindOfClass:[ViewControllerYouWantToStopOn class]]){
[[self navigationController] popToViewController:obj animated:YES];
return;
}
}
//For Push
NSArray *viewControllers = [[self navigationController] viewControllers];
for( int i=0;i<[viewControllers count];i++){
id obj=[viewControllers objectAtIndex:i];
if([obj isKindOfClass:[ViewControllerYouWantToJumpOn class]]){
[[self navigationController] pushViewController:obj animated:YES];
return;
}
}
Upvotes: 0