iJazJaz
iJazJaz

Reputation: 45

how to move between segues in navigation controller?

In my story board I have:

NavController-> View1(root) -> View2 ->View3 ->View4

I can normally use the flow in that order and navigation between segues is easy.

But How to go from View1 to View4 .. and from View4 there is back button should take you to View3? Because if I made direct Segue From View1 to View4 ,, I cant reach View3.

Please let me know how to do this. Thanks in advance.

Upvotes: 0

Views: 65

Answers (2)

Y.Bonafons
Y.Bonafons

Reputation: 2349

Here is an example with 3 viewcontrollers only but it will be the same logic:

You need to create segue between viewcontrollers like this:

  • viewController1 --- segueFrom1To2 ----- viewController2
  • viewController1 --- segueFrom1To3 ----- viewController3
  • viewController2 --- segueFrom2To3 ----- viewController3

Then you need a property in your viewController2, let's say: goToNextVc; So, in your viewController1, you will be able to set this property when your perform the appropriate segue:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

    ViewController2 *vc2 = segue.destinationViewController;

    if ([segue.identifier isEqualToString:@"segueFrom1To3"])
    {
        vc2.goToNextVc = YES;
    }
    else{
        vc2.goToNextVc = NO;
    }
}

And now in the viewWillAppear of viewController2, simply add:

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

    if (self.goToNextVc){
        self.goToNextVc = NO;
        [self performSegueWithIdentifier:@"segueFrom2To3" sender:self];
    }
}

If you don't want to see the transition between viewController1 and viewController2, simply create a custom segue, let's say SegueNoTransition in which you need to override perform:

-(void) perform{
    [[[self sourceViewController] navigationController] pushViewController:[self destinationViewController] animated:NO];
}

And replace the type "show" of segueFrom1To3 with "custom" and the class with SegueNoTransition;

Upvotes: 1

Jayesh Mardiya
Jayesh Mardiya

Reputation: 780

    for (UIViewController *controller in self.navigationController.viewControllers) {

                    if ([controller isKindOfClass:[yourViewController class]]) {
                        [self.navigationController popToViewController:controller
                                                              animated:YES];
                    }
                    else {
                         NSLog(@"Not Found");
                    }
}                
    // You must import the viewController where you want to move.

    // yourViewController = viewController where you want to go in example.

Upvotes: 0

Related Questions