我正在尝试创建一个容器视图控制器来将 UINavigationController 和自定义 UIViewController 一起保存在屏幕上。
我把它放在一个测试程序中,它工作得很好,但是当我试图在我的真实项目中实现它时,包含视图控制器的外观方法永远不会被调用。
工作测试:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
MasterStatusViewController *master = [[MasterStatusViewController alloc] init];
[self.window setRootViewController:master];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
- (void)viewDidLoad {
[super viewDidLoad];
InventoryViewController *newInventory = [[InventoryViewController alloc] init];
self.navigation = [[UINavigationController alloc] initWithRootViewController:newInventory];
self.statusRibbon = [[StatusBarViewController alloc] initWithNibName:@"StatusBarViewController" bundle:nil];
[self addChildViewController:self.navigation];
self.navigation.view.frame = self.navigationView.frame;
[self.view addSubview:self.navigation.view];
[self.navigation didMoveToParentViewController:self];
[self addChildViewController:self.statusRibbon];
self.statusRibbon.view.frame = self.ribbonView.frame;
[self.view addSubview:self.statusRibbon.view];
[self.statusRibbon didMoveToParentViewController:self];
}
失败的项目:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
MasterViewController *master = [[MasterViewController alloc] init];
self.centerController = master;
[self.window setRootViewController:master];
[self.window makeKeyAndVisible];
return YES;
}
- (void)viewDidLoad {
[super viewDidLoad];
LoginViewController *login = [[LoginViewController alloc] init];
self.navigation = [[UINavigationController alloc] initWithRootViewController:login];
self.statusRibbon = [[StatusRibbonViewController alloc] init];
[self addChildViewController:self.navigation];
self.navigation.view.frame = self.navigationView.frame;
[self.view addSubview:self.navigation.view];
[self.navigation didMoveToParentViewController:self];
[self addChildViewController:self.statusRibbon];
self.statusRibbon.view.frame = self.ribbonView.frame;
[self.view addSubview:self.statusRibbon.view];
[self.statusRibbon didMoveToParentViewController:self];
}
视图排列正确并按预期执行,除了外观方法永远不会被调用。我很困惑。手动调用这些方法似乎不是一个解决方案,因为这些调用也没有通过。
编辑:外观方法是指 viewWillAppear、viewDidAppear 等。
第二次编辑:
~剪断~
第三次编辑:经过进一步检查,我相信没有调用外观生命周期方法的原因是因为它们也没有在容器视图控制器上被调用。我添加了
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSLog(@"MasterViewController VIEW WILL APPEAR");
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSLog(@"MasterViewController VIEW DID APPEAR");
}
to the masterViewController
and nothing,由于某种原因,它们从未被调用过。