问题
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