ritch
ritch

Reputation: 1808

Problems implementing a container view

I'm trying to follow the View Controller Programming Guide for iOS to implement a container view in my application. At the moment I'm just trying to get an initial first view to load but the label included in the first controller is not being shown. In the end, I hope to be able to control which view is shown in the container by using a segmented control.

Any help would be greatly appreciated.

My Storyboard

Storyboard Screenshot

ViewController.h

@interface ViewController : UIViewController

@property (weak, nonatomic) IBOutlet UIView *contentController;

- (IBAction)SegmentedControlValueChange:(UISegmentedControl *)sender;

@end

ViewController.m

#import "ViewController.h"

#import "FirstController.h"
#import "SecondController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    FirstController *firstController = [[FirstController alloc] init];
    [self displayContentController:firstController];
}

- (IBAction)SegmentedControlValueChange:(UISegmentedControl *)sender
{
}

- (void)displayContentController: (UIViewController *)content
{
    [self addChildViewController:content];

    content.view.frame = [self frameForContentController];

    [self.view addSubview:content.view];

    [content didMoveToParentViewController:self];
}

- (CGRect)frameForContentController
{
    return self.contentController.bounds;
}

@end

Upvotes: 3

Views: 1107

Answers (1)

John Grant
John Grant

Reputation: 1679

If FirstController is part of the storyboard, then you'll have to load it from the storyboard.

Try doing

FirstController *firstController = [self.storyboard instantiateViewControllerWithIdentifier:@"yourIdentifier"];

Upvotes: 3

Related Questions