简体   繁体   中英

iOS: UIViewController doesn't display another UIViewController using storyboards

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM