viewDidDisappear not getting called on a UINavigat

2019-03-06 15:56发布

问题:

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!

回答1:

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.



回答2:

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:

You must call super at implementation of viewWillDisappear.



回答4:

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.