ximmyxiao
ximmyxiao

Reputation: 2811

How to set segue doing pop to root and push a new one in storyboard?

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

Answers (3)

Mr.Paroli
Mr.Paroli

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

Arpit Jain
Arpit Jain

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.

https://developer.apple.com/library/content/technotes/tn2298/_index.html#//apple_ref/doc/uid/DTS40013591-CH1-TNTAG2-ADDING_AN_UNWIND_SEGUE_TO_A_STORYBOARD

For pushing to new VC, You can simply push in storyboard.

Upvotes: 2

Vineet Rai
Vineet Rai

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

Related Questions