Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>A couple of thoughts:</p> <ol> <li><p>If you want to push from VC2 to either VC3 or to VC4, rather than having VC3 immediately push to VC4 in special cases, I think it's better to just have VC2 push directly to the appropriate view controller. Thus you might have an <code>IBAction</code> in VC2 that would do this for you:</p> <pre><code>- (IBAction)pushToNext:(id)sender { BOOL skipToVC4 = ... // put in whatever logic you'd use to bypass VC3 and go directly to VC4 UIViewController *nextController; if (skipToVC4) { nextController = [self.storyboard instantiateViewControllerWithIdentifier:@"VC4"]; // obviously, if using NIBs, you'd do something like: // nextController = [[ViewController4 alloc] initWithNibName:@"VC4" bundle:nil]; } else { nextController = [self.storyboard instantiateViewControllerWithIdentifier:@"VC3"]; } [self.navigationController pushViewController:nextController animated:YES]; } </code></pre> <p>This way, when you pop back from VC4, you'll pop back directly to the appropriate view controller (e.g. if you pushed from VC2 to VC4, when you pop, you'll pop right back to VC2 automatically.</p> <p>And, obviously, if you're using storyboards, rather than manually invoking <code>pushViewController</code>, you could have two segues from VC2 (one to VC3 and one to VC4), give them appropriate identifiers, and then just invoke <code>performSegueWithIdentifier</code> to segue to the appropriate view controller. But the idea is the same: You can define an <code>IBAction</code> that performs the appropriate segue depending upon whatever logic you so choose.</p></li> <li><p>You say that you have "PREVIOUS and NEXT buttons in the nav bar that pushes my views", I wonder about your "PREVIOUS" button. Is that doing a <code>popViewControllerAnimated</code>? Generally, a "NEXT" button will push to a new view controller, but the "PREVIOUS" button should not push to the previous view, but pop back to it. If you don't pop back, you can end up with multiple instances of some of your prior view controllers. Thus, the "PREVIOUS" button should be linked to an <code>IBOutlet</code> that does something like:</p> <pre><code>- (IBAction)popToPrevious:(id)sender { [self.navigationController popViewControllerAnimated:YES]; } </code></pre></li> <li><p>When popping back, you'll obviously pop back to the view controller that you pushed from. If you want to skip a few of the view controllers as you're popping back in iOS versions prior to 6.0, you would use <code>popToViewController</code> or <code>popToRootViewControllerAnimated</code>. For example, let's say that you pushed from VC1 to VC2, to VC3, to VC4. If you want to pop back from VC4 all the way to VC1, you would hook up and <code>IBAction</code> in VC4 like:</p> <pre><code>- (IBAction)popToRoot:(id)sender { [self.navigationController popToRootViewControllerAnimated:YES]; } </code></pre> <p>Or, if you wanted to pop back from VC4 to VC2, you would</p> <pre><code>- (IBAction)popToVC2:(id)sender { for (UIViewController *controller in self.navigationController.viewControllers) { if ([controller isKindOfClass:[ViewController2 class]]) { [self.navigationController popToViewController:controller animated:YES]; return; } } } </code></pre> <p>You can avoid this iteration through the <code>navigationController.viewControllers</code> if you passed a reference of VC2 to VC3 and then again to VC4, but sometimes the above technique is easier.</p> <p>By the way, if you're supporting iOS 6 and above, only, and are using storyboards, you can also use unwind segues, which are a more elegant way of popping back to a particular view controller. But it's not clear whether (a) you're using storyboards; and (b) you're supporting iOS 6 and above only, so I'll refrain from a discussion of unwind segues at this point.</p></li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload