Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The Storyboard image is a little misleading here.</p> <p>When you segue to B, <em>actually</em> you are segueing to the B/C combo as NavControllers always have at least one viewController in their stack (which is their <code>topViewController</code> and their <code>[viewControllers objectAtIndex:0]</code>).</p> <p>So you do have a relationship directly from A to C.</p> <p>How you access that controller depends on whether your segue is <em>modal</em> or <em>push</em>. In your case it is modal, but I will describe both so you can see the difference.</p> <p>In either case, to pass data to C, you need to declare a property in it's header file </p> <pre><code> @interface CviewController: UIViewContrller @property (assign) int dataFromA; @end </code></pre> <p><strong>push segue</strong> </p> <p>In a push segue, it is actually C that is the destinationViewController, not B. In fact the push segue is governed by B, which is the UINavigationController for both A and C. The code behind the push segue is of the form<br> <code>[self.navigationController pushViewController:otherViewController];</code></p> <p>In AviewController's prepareForSegue:</p> <pre><code> - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { CviewController* controller = segue.destinationViewController; [controller setDataFromA:self.data]; } </code></pre> <p>It is <em>possible</em> in the storyboard to make a push segue line between two viewControllers that do <em>not</em> share a common UINavigationController. However when you run this you will get a crash error:</p> <blockquote> <p>'Could not find a navigation controller for segue 'pushC'. Push segues can only be used when the source controller is managed by an instance of UINavigationController.'</p> </blockquote> <p>Behind every good push segue lies a Navigation Controller.</p> <p><strong>modal segue</strong> </p> <p>The code hiding behind a modal Segue is the UIViewController method<br> <code>- (void)presentViewController:(UIViewController *)viewControllerToPresent</code> </p> <p>In a modal segue to a NavController/ViewController combo, the destination viewController is whatever the segue line points to. If it points to a viewController, that is the segue.destinationController (and the UINavigationController will be ignored, which is not what you want here); if it points to a UINavigationController, as in this case, <em>that</em> will be it's destinationController. But it is still straightforward to access the viewController, as it will be the navigation Controller's topViewController.</p> <p>In AviewController's prepareForSegue:</p> <pre><code>- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { CviewController* controller = (CviewController*)[[segue destinationViewController] topViewController]; [controller setDataFromA:self.data]; } </code></pre> <p>Note that in this case we have to use old-style [[message passing] syntax]. If we use modern.property.syntax we get a compile error. That's because the program does not know the type of desinationViewController, and refuses to accept topViewController as a property of an unknown type. But it <em>is</em> happy to [send [real messages]] to an unknown type. We also have to (typecast*) to avoid compiler warnings. </p>
 

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