Container View Controller不转发外观方法

时间:2013-01-31 17:17:40

标签: objective-c xcode uiviewcontroller

我正在尝试创建一个容器视图控制器,以便在屏幕上同时保存UINavigationController和自定义UIViewController。

我在测试程序中完成了它并且它完美地工作,但是当我尝试在我的真实项目中实现它时,包含View控件的外观方法永远不会被调用。

工作测试:

- (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等。

第二次编辑:

~snip~

第三次编辑:经过进一步检查,我认为没有调用外观生命周期方法的原因是因为它们也没有在容器视图控制器上调用。我添加了

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSLog(@"MasterViewController VIEW WILL APPEAR");
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    NSLog(@"MasterViewController VIEW DID APPEAR");
}

masterViewController而没有任何东西,由于某种原因,他们永远不会被召唤。

3 个答案:

答案 0 :(得分:5)

解决了它。我不知道为什么没有调用主外观方法,xcode的一些清理似乎解决了这个问题。

但是,这些方法仍未转发给我的其他视图控制器。然后我偶然发现this link并且知道无论怎样,你总是要向他们转发外观。我能够通过添加:

来修复它
- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];
    [self.navigation beginAppearanceTransition: YES animated: animated];
}

- (void)viewDidAppear:(BOOL)animated {

    [super viewDidAppear:animated];
    [self.navigation endAppearanceTransition];
}

-(void) viewWillDisappear:(BOOL)animated {

    [super viewWillDisappear:animated];
    [self.navigation beginAppearanceTransition: NO animated: animated];
}

-(void) viewDidDisappear:(BOOL)animated {

    [super viewDidDisappear:animated];
    [self.navigation endAppearanceTransition];
}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

答案 1 :(得分:3)

我有一个类似的问题,我在使用UIViewController的包含功能,每当我向视图添加一个新的子视图控制器时,外观方法都没有转发。

事实证明,在完成后添加子视图和endApperanceTransition之前,需要调用beginAppearanceTransition:animated :.添加子视图时以及删除子视图以触发外观方法时,需要执行此操作。

对于你的情况,我会尝试这样的事情:

self.navigation.view.frame = self.navigationView.frame;
[self addChildViewController:self.navigation];
[self.navigation didMoveToParentViewController:self]
[self.navigation beginAppearanceTransition:YES animated:NO];
[self.view addSubview:self.navigation.view];
[self.navigation endAppearanceTransition];

self.statusRibbon.view.frame = self.ribbonView.frame;
[self addChildViewController:self.statusRibbon];
[self.statusRibbon didMoveToParentViewController:self]
[self.statusRibbon beginAppearanceTransition:YES animated:NO];
[self.view addSubview:self.statusRibbon.view];
[self.statusRibbon endAppearanceTransition];

答案 2 :(得分:0)

“外观方法”究竟是什么意思?你的意思是,视图没有出现?

在您的工作示例中,您正在从nib文件加载功能区视图:

self.statusRibbon = [[StatusBarViewController alloc] initWithNibName:@"StatusBarViewController" bundle:nil];

而你在第二个代码示例中只是init

self.statusRibbon = [[StatusRibbonViewController alloc] init];

后者只会创建一个“新鲜”的视图,这可能会导致您的感知,但它不会出现。