viewWillAppear / viewDidappear methods aren't called.

本文介绍如何通过实现UINavigationControllerDelegate协议确保在导航控制器堆栈上推入或弹出视图控制器时始终调用viewWillAppear和viewDidAppear方法。同时,讨论了手动调用viewWillDisappear和viewDidDisappear方法的必要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

When you push or pop a view controller on/off a navigaction controller's stack, the usual viewWillAppear / viewDidappear methods aren't called. If you want to ensure they're always called, just add the UINavigationControllerDelegate protocol to your root view controller:

@interface RootViewController : UIViewController <UINavigationControllerDelegate> {
    UINavigationController *navController;
}

Then implement these two methods:

- (void)navigationController:(UINavigationController *)navigationController 
    willShowViewController:(UIViewController *)viewController animated:(BOOL)animated 
{
    [viewController viewWillAppear:animated];
}

- (void)navigationController:(UINavigationController *)navigationController 
    didShowViewController:(UIViewController *)viewController animated:(BOOL)animated 
{
    [viewController viewDidAppear:animated];
}

Be sure to set the root view controller as the delegate for the nav controller. Now viewWillAppear / viewDidAappear will be called whenever a controller is pushed/popped from the stack.

If you want to call the viewWillDisappear/viewDidDisappear methods, your view controller still has to do that manually before popping itself off the nav stack.

转载于:https://www.cnblogs.com/rothwell/archive/2012/04/09/2439681.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值