Note that there are some explanatory texts on larger screens.

plurals
  1. POThe ultimate guide for writing parent view controllers
    text
    copied!<h1>Short version</h1> <h3>If I'm writing my own custom split view controller, what do I need to do to make the child view controllers work as expected?</h3> <p>For instance: to send <code>viewWillAppear:</code> and so on.</p> <h1>Long version</h1> <h2>Background</h2> <p>A while back I answered the following question:<br> <a href="https://stackoverflow.com/questions/5555218/switch-between-uiviewcontrollers-using-uisegmentedcontrol/5555729">Switch between UIViewControllers using UISegmentedControl</a></p> <p>With the following answer:</p> <blockquote> <p>The right way to do it is to have the controller handling the <code>UISegmentedControl</code> add the views of the controllers as subviews.</p> <pre><code>[self.view addSubview:controller.view]; </code></pre> <p>It's your responsibility to send <code>viewWillAppear:</code> and so on.</p> </blockquote> <p>However, tc. pointed out that this is not as trivial as it sounds:</p> <blockquote> <p>No. View controllers are not meant to be used like that - controller will miss out on a lot of the UIViewController magic that's taken for granted (namely -view{Will,Did}{Appear,Disappear}: and -shouldRotateToViewOritentation:).</p> <p>By "magic", I'm referring to everything UIKit does behind the scenes. You also forgot -parentViewController (which is important for things like modal view controllers). Additionally, somewhere in the depths of UIKit, it automatically calls -viewSomethingSomething: for you, so you might get -viewDidDisappear: twice! (I can't remember the exact details, but there's another user reporting that all you need to do is call -viewWillAppear: and the other three methods happen automatically.) The key issue is that Apple doesn't document the "magic" or how it changes between OS updates.</p> </blockquote> <p>Since then I've been thinking that one should compile a guide for what needs to be present in the parent view controller in order for the child view controllers to work as expected. Apple's documentation doesn't cover this and Google didn't help much either, so now I'm hoping to find this knowledge within the stackoverflow community. </p> <p>I've written several controllers like this and they all seem to work, but I can't help but wonder if I'm missing out on important view controller magic.</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