95

Can anyone point me to any good examples of creating a Custom View Controller as a Container View Controller? The only documentation I can find is a couple of paragraphs in the UIViewController Class Reference. I feel I need a little more information than that and an example implementation would be nice. Google has turned up nothing at all.

I am specifically interested in the method:

transitionFromViewController:toViewController:duration:options:animations:completion:
Cœur
  • 37,241
  • 25
  • 195
  • 267
Undistraction
  • 42,754
  • 56
  • 195
  • 331

7 Answers7

52

The best thing I have found so far is the WWDC 2011 Session Video Session 102 - Implementing UIViewController Containment.

Max MacLeod
  • 26,115
  • 13
  • 104
  • 132
hypercrypt
  • 15,389
  • 6
  • 48
  • 59
37

In addition to the WWDC Session Video Session 102 - Implementing UIViewController Containment that hypercrypt already mentioned, Apple WWDC 2012 session on "The Evolution of View Controllers on iOS" also covers this topic and the example code is part of the sample code package:

https://developer.apple.com/devcenter/download.action?path=/wwdc_2012/wwdc_2012_sample_code/wwdc_2012_session_code.dmg

There's also an example here: https://github.com/toolmanGitHub/stackedViewControllers

JosephH
  • 37,173
  • 19
  • 130
  • 154
17
- (void)viewDidLoad{
    [super viewDidLoad];

    // I put self in a Navigation VC so we can use its right navigationbar 
    // item for triggering the transition
    self.navigationItem.rightBarButtonItem = 
     [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit 
                                                    target:self 
                                                    action:@selector(button:)] 
                                                                  autorelease];

    // create test1 and test2 instance (subclass UIViewController and 
    // also need to define their own nibs)
    vc1 = [[test1 alloc]initWithNibName:@"test1" bundle:nil];
    vc2 = [[test2 alloc]initWithNibName:@"test2" bundle:nil];

    //add to the container vc which is self    
    [self addChildViewController:vc1];
    [self addChildViewController:vc2];

    //the entry view (will be removed from it superview later by the api)
    [self.view addSubview:vc1.view];
}

this IBAction triggers the transition between two VCs:

-(IBAction)button:(id)sender {
    [self transitionFromViewController:vc1 
                      toViewController:vc2 
                              duration:0.5    
                               options:UIViewAnimationOptionTransitionCurlDown 
                            animations:nil 
                            completion:nil];
}
Alex Cio
  • 6,014
  • 5
  • 44
  • 74
sonnywang
  • 292
  • 1
  • 8
  • 1
    This is a great example. But the memory management in this case can be improved: http://stackoverflow.com/a/8453677/849616 for more suitable solution. – Nat Sep 26 '12 at 14:16
  • 1
    Not that you're wrong. But [self addChildViewController:vc1]; doesn't make sense. The Container VC may have SEVERAL Container View Controllers. – user4951 Oct 10 '12 at 06:00
  • @Vive I have reached a point in my project where I am concerned about memory management. Could you tell me what particularly the link you posted does better in terms of memory management? I am very new and have a hard time seeing the discrepancies – jacobronniegeorge Jan 11 '13 at 20:37
11

I found this example very useful for me

http://sandmoose.com/post/35714028270/storyboards-with-custom-container-view-controllers

And they have source on github:

https://github.com/mluton/EmbeddedSwapping

Yuri Solodkin
  • 530
  • 8
  • 26
10

Could this:

http://subjective-objective-c.blogspot.com/2011/08/writing-high-quality-view-controller.html

Be enough for your needs?

Rui Peres
  • 25,741
  • 9
  • 87
  • 137
  • 4
    Actually I had already found that, but unfortunately it is written about Ios4, but 5 has made significant changes. Apple now explicitly support creating custom View Controller Containers, though they don't seem to want to help us work out how to use them. Thanks though. – Undistraction Oct 13 '11 at 15:08
  • Can you show me where they say that. Not doubting you, just curious, because I have been struggling with that too. – Rui Peres Oct 13 '11 at 15:13
  • 4
    No worries. I looked through the code for his container classes and none of them use any of the following methods: addChildViewController:, removeFromParentViewController, transitionFromViewController:toViewController:duration:options:animations:completion:, willMoveToParentViewController: and didMoveToParentViewController: Which are all mentioned in the updated ios5 Documentation for UIViewController. – Undistraction Oct 13 '11 at 15:28
8

don't know if this is a "good" example, but you can get a free Container ViewController from https://bitbucket.org/javieralonso/jaacordeonviewcontroller/overview

It's a full accordion metaphor container view controller

javieralog
  • 1,337
  • 9
  • 10
3

These are my favorite (iOS7-ready) tutorial / examples on the subject (all three have source code available on github):

View Controller Containment

Custom Container View Controller Transitions

Interactive Custom Container View Controller Transitions

And then, of course, Apple offers a whole write-up on the subject which I find invaluable:

Creating Custom Container View Controllers

radiovisual
  • 6,298
  • 1
  • 26
  • 41