0

我正在尝试创建一个容器视图控制器来将 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 masterViewControllerand nothing,由于某种原因,它们从未被调用过。

4

3 回答 3

5

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

但是,这些方法仍然没有被转发到我的其他视图控制器。然后我遇到了这个链接,并了解到无论如何你都必须将外观呼叫转发给他们。我能够通过添加来修复它:

- (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.
}
于 2013-02-01T19:21:13.193 回答
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];
于 2013-09-11T22:19:00.037 回答
0

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

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

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

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

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

后者只是创造了一个“新鲜”的观点,这可能会导致你认为它没有出现。

于 2013-01-31T18:15:52.260 回答