温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

iphone开发之解决viewWillAppear失效

发布时间:2020-03-27 08:44:13 来源:网络 阅读:3333 作者:arthurchen 栏目:开发技术

你可曾遇到过viewWillAppear没有被调用到的情况

产生原因是用了UINavigationController. 
将UINavigationController的view作为subview添加到了其他viewController的view中。
或者把UINavigationController添加到UITabbarController中了。

此时,NavigationController的stack里面的viewController就收不到-(void)viewWillAppear:(BOOL)animated;等4个方法的调用。

 

原因呢

 

Apple Docs state:

 

Warning: If the view belonging to a view controller is added to a view hierarchy directly, the view controller will not receive this message. If you insert or add a view to the view hierarchy, and it has a view controller, you should send the associated view controller this message directly. Failing to send the view controller this message will prevent any associated animation from being displayed.

不过后面的到4.0的文档就没有发现这样的文字描述了,但是还是没能够调用的到这样

只是添加的更复杂的文档,头晕晕看不下去了。

 

解决方法两种:
1,在导航控制器上层controller的viewWillAppear中显式调用viewWillAppear方法。

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

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

2,把导航控制器上层controller设为UINavigationController的delegate

nav.delegate = self;

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

 

- (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]; }

凑合着用吧,我喜欢第二种,不过我更喜欢干脆不再viewWillappear里做事情了。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI