Problems implementing a container view

2019-02-19 03:34发布

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

1条回答
Rolldiameter
2楼-- · 2019-02-19 04:05

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"];

查看更多
登录 后发表回答