viewDidDisappear not getting called on a UINavigat

2019-03-06 16:18发布

I'm having a problem with my navigation controller. If I add a view controller to the stack:

- (void) tui_ToggleListStudy:(id)sender
{
    listVC = [[ListViewController alloc] init];
    [self.navigationController pushViewController:listVC animated:NO];
    [listVC release];
}

I have NSLog messages for the view controller beneath, for both viewWillDisappear: and viewDidDisappear - but only viewWillDisappear: is getting called.

Not only that, but the view controller doesn't receive any other delegate messages either: No viewDidUnload, or dealloc...

Is there anything I can do about this?

I'm stumped! Any thoughts?

Thanks!

4条回答
The star\"
2楼-- · 2019-03-06 16:27

Assuming your navigation controller is contained in some kind of top view controller, you must also forward the relevant messages from that top view controller to the nav controller:

-(void)viewWillAppear:(BOOL)animated
{
    [navController viewWillAppear:animated];
}

-(void)viewDidAppear:(BOOL)animated
{
    [navController viewDidAppear:animated];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [navController viewWillDisappear:animated];
}

-(void)viewDidDisappear:(BOOL)animated
{
    [navController viewDidDisappear:animated];
}
查看更多
看我几分像从前
3楼-- · 2019-03-06 16:28

You must call super at implementation of viewWillDisappear.

查看更多
Explosion°爆炸
4楼-- · 2019-03-06 16:37

I know the answer if you made the same typo in your code that you made in your question: the method signature is viewDidDisappear: (with the animated argument), not viewDidDisappear.

Not only that, but the view controller doesn't receive any other delegate messages either: No viewDidUnload, or dealloc...

A view controller will not be deallocated when you push another controller onto the stack. And viewDidUnload won't be called unless you run out of memory.

查看更多
家丑人穷心不美
5楼-- · 2019-03-06 16:46

The designated initializer for UIViewController is -initWithNibName:bundle:. Are you sure your view controller is finding its nib and finds its connected view? I'll bet if you set a breakpoint after init'ing your ListViewController, you'll find its -view returns nil.

查看更多
登录 后发表回答