0

I am tryin to display multiple UIViewController objects inside a single view. For the time being I want to display a single UIViewController object when the app loads. But the app screen appears blank, while it should be displaying a label inside the child view controller.

Here is what I did:

ParentViewController.h

#import <UIKit/UIKit.h>
@interface ParentViewController : UIViewController
{
    UIViewController *child1Controller;
    UIViewController *child2Controller;
}
@end

ParentViewController.m

#import "ParentViewController.h"
#import "Child1Controller.h"
#import "Child2Controller.h"

@interface ParentViewController ()
@end

@implementation ParentViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { ... }

- (void)viewDidLoad
{
    child2Controller = [[Child2Controller alloc] init];
    [self.view addSubview:child2Controller.view];

    [super viewDidLoad];
    // Do any additional setup after loading the view.

}

- (void)viewDidUnload { ... }

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { ... }

@end

Then in the storyboard in interface builder

  • add 3 view controllers
  • assigned a class to each one of them ParentViewController, Child1Controller & Child2Controller
  • in Child2Controller object, added a UILabel inside View.
  • in Child2Controller.h defined the IBOutlet for UILabel and added a synthesize statement for the same in Child2Controller.m
  • finally in project-Info.plist set the main storyboard file

Did I miss something over here?

vikmalhotra
  • 9,981
  • 20
  • 97
  • 137

1 Answers1

3

Starting from iOS 5 it's possible to take advantage of View Controller Containment. This is a new methodology that allows you to create a custom controller container like UINavigationController or UITabBarController.

In your case, this could be very useful. In fact, in your storyboard you could create the parent controller and the two child controllers. The parent could be linked to another scene while the two children are not linked. They are independent scenes that you can use within your parent controller.

For example in viewDidLoad method of your parent controller you could do the following:

- (void)viewDidLoad
{
   [super viewDidLoad];

   UIStoryboard *storyboard = [self storyboard];

   FirstChildController *firstChildScene = [storyboard instantiateViewControllerWithIdentifier:@"FirstChildScene"];
   [self addChildViewController:firstChildScene];
   [firstChildScene didMoveToParentViewController:self];
}

Then in your FirstChildController override didMoveToParentViewController

- (void)didMoveToParentViewController:(UIViewController *)parent
{
   // Add the view to the parent view and position it if you want
   [[parent view] addSubview:[self view]];
   CGRect newFrame = CGRectMake(0, 0, 350, 400);
   [[self view] setFrame:newFrame];
}

And voilà! You have a controller that contains one view that is managed by a child controller.

For further info see how-does-view-controller-containment-work-in-ios-5.

Hope it helps.

Community
  • 1
  • 1
Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
  • +1 this indeed works just fine. But what I'd like to figure out is what I had done wrong in the code/flow I has posted in question. – vikmalhotra Apr 26 '12 at 02:21
  • @ShiVik What about Child2Controller? Is it loaded with a xib or did you override *loadView* method? – Lorenzo B Apr 26 '12 at 07:46
  • No I didn't create a new xib file nor `loadView`. What I did was that I added a UIViewController in storyboard and assigned its custom class to be `Child2Controller` in the identity inspector. – vikmalhotra Apr 26 '12 at 08:12
  • Oops, sorry I forgot you use storyboards...My fault..Try to use *[storyboard instantiateViewControllerWithIdentifier:@"YourSceneIdentifier"];* to instantiate the controller instead of alloc-init pattern and let me know. – Lorenzo B Apr 26 '12 at 08:26
  • :) I have done that already. It works as I said in my first comment. I just wanted to find out what was wrong in the flow I have described in the question. – vikmalhotra Apr 26 '12 at 08:30
  • 1
    @ShiVik Ok but if you create that UIViewController with alloc-init you bypass the storyboard and you cannot link the controller with the storyboard created interface. In other words, if you use alloc-init you create a new controller with an empty view. This is why the screen is blank. Hope it helps. – Lorenzo B Apr 26 '12 at 08:39