Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When you have child views that have their own view controllers, you should be following the custom container controller pattern. See <a href="https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/ImplementingaContainerViewController.html#//apple_ref/doc/uid/TP40007457-CH11-SW1" rel="noreferrer">Creating Custom Container View Controllers</a> for more information.</p> <p>Assuming you've followed the custom container pattern, when you want to change the child view controller (and its associated view) for the "content view", you would do that programmatically with something like:</p> <pre><code>UIViewController *newController = ... // instantiate new controller however you want UIViewController *oldController = ... // grab the existing controller for the current "content view"; perhaps you maintain this in your own ivar; perhaps you just look this up in self.childViewControllers newController.view.frame = oldController.view.frame; [oldController willMoveToParentViewController:nil]; [self addChildViewController:newController]; // incidentally, this does the `willMoveToParentViewController` for the new controller for you [self transitionFromViewController:oldController toViewController:newController duration:0.5 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{ // no further animations required } completion:^(BOOL finished) { [oldController removeFromParentViewController]; // incidentally, this does the `didMoveToParentViewController` for the old controller for you [newController didMoveToParentViewController:self]; }]; </code></pre> <p>When you do it this way, there's no need for any delegate-protocol interface with the content view's controller (other than what iOS already provides with the <a href="https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/index.html#//apple_ref/doc/uid/TP40006926-CH3-SW86" rel="noreferrer">Managing Child View Controllers in a Custom Container</a> methods).</p> <hr> <p>By the way, this assumes that the initial child controller associated with that content view was added like so:</p> <pre><code>UIViewController *childController = ... // instantiate the content view's controller any way you want [self addChildViewController:childController]; childController.view.frame = ... // set the frame any way you want [self.view addSubview:childController.view]; [childController didMoveToParentViewController:self]; </code></pre> <hr> <p>If you want a child controller to tell the parent to change the controller associated with the content view, you would:</p> <ol> <li><p>Define a protocol for this:</p> <pre><code>@protocol ContainerParent &lt;NSObject&gt; - (void)changeContentTo:(UIViewController *)controller; @end </code></pre></li> <li><p>Define the parent controller to conform to this protocol, e.g.:</p> <pre><code>#import &lt;UIKit/UIKit.h&gt; #import "ContainerParent.h" @interface ViewController : UIViewController &lt;ContainerParent&gt; @end </code></pre></li> <li><p>Implement the <code>changeContentTo</code> method in the parent controller (much as outlined above):</p> <pre><code>- (void)changeContentTo:(UIViewController *)controller { UIViewController *newController = controller; UIViewController *oldController = ... // grab reference of current child from `self.childViewControllers or from some property where you stored it newController.view.frame = oldController.view.frame; [oldController willMoveToParentViewController:nil]; [self addChildViewController:newController]; [self transitionFromViewController:oldController toViewController:newController duration:1.0 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{ // no further animations required } completion:^(BOOL finished) { [oldController removeFromParentViewController]; [newController didMoveToParentViewController:self]; }]; } </code></pre></li> <li><p>And the child controllers can now use this protocol in reference to the <code>self.parentViewController</code> property that iOS provides for you:</p> <pre><code>- (IBAction)didTouchUpInsideButton:(id)sender { id &lt;ContainerParent&gt; parentViewController = (id)self.parentViewController; NSAssert([parentViewController respondsToSelector:@selector(changeContentTo:)], @"Parent must conform to ContainerParent protocol"); UIViewController *newChild = ... // instantiate the new child controller any way you want [parentViewController changeContentTo:newChild]; } </code></pre></li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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