我正在尝试在单个视图中显示多个 UIViewController 对象。目前我想在应用加载时显示一个 UIViewController 对象。但是应用程序屏幕显示为空白,而它应该在子视图控制器内显示一个标签。
这是我所做的:
父视图控制器.h
#import <UIKit/UIKit.h>
@interface ParentViewController : UIViewController
{
UIViewController *child1Controller;
UIViewController *child2Controller;
}
@end
父视图控制器.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
然后在界面生成器的情节提要中
- 添加 3 个视图控制器
- 为它们中的每一个分配了一个类 ParentViewController、Child1Controller 和 Child2Controller
- 在 Child2Controller 对象中,在 View 中添加了一个 UILabel。
- 在 Child2Controller.h 中为 UILabel 定义了 IBOutlet 并在 Child2Controller.m 中添加了相同的综合语句
- 最后在 project-Info.plist 中设置主故事板文件
我在这里错过了什么吗?