Problems implementing a container view

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-21 08:40:09

问题


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

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:


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



来源:https://stackoverflow.com/questions/24886770/problems-implementing-a-container-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!