iphone开发之解决viewWillAppear失效

简介:

你可曾遇到过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里做事情了。










本文转自 arthurchen 51CTO博客,原文链接:http://blog.51cto.com/arthurchen/575678,如需转载请自行联系原作者
目录
相关文章
|
4月前
|
编解码 测试技术 iOS开发
iPhone 屏幕尺寸和开发适配
【10月更文挑战第23天】iPhone 的屏幕尺寸变化给开发者带来了一定的挑战,但也为创新提供了机遇。通过深入了解不同屏幕尺寸的特点,遵循适配原则和策略,运用合适的技巧和方法,我们能够为用户提供在不同 iPhone 机型上都具有良好体验的应用。在未来,随着技术的不断进步,我们还需要持续学习和适应,以满足用户对优质应用体验的不断追求。
|
4月前
|
编解码 iOS开发 UED
响应式设计在 iPhone 开发适配中的具体应用
【10月更文挑战第23天】响应式设计在 iPhone 开发适配中扮演着至关重要的角色,它能够帮助我们打造出适应不同屏幕尺寸和用户需求的高质量应用。通过合理运用响应式设计的原则和方法,我们可以在提供良好用户体验的同时,提高开发效率和应用的可维护性。
|
7月前
|
数据采集 iOS开发 Python
Chatgpt教你开发iPhone风格计算器,Python代码实现
Chatgpt教你开发iPhone风格计算器,Python代码实现
77 1
|
Shell iOS开发
iOS逆向:tweak开发教程(iPhone/tool)
iOS逆向:tweak开发教程(iPhone/tool)
1294 0
iOS逆向:tweak开发教程(iPhone/tool)
|
编解码 iOS开发
iphone 开发的基本入门知识
iphone 开发的基本入门知识
268 0
「镁客早报」iPhone或将在今年采用三摄;传Facebook致力于开发语音助力服务与亚马逊、苹果竞争
亚马逊向美国Alexa设备推免费音乐服务;视频会议软件开发商Zoom纳斯达克上市。
286 0
|
Web App开发 缓存 开发工具