问题描述
我读过很多关于当您没有正确创建视图层次结构时viewWillAppear
出现问题的帖子.恰到好处.我的问题是我不明白这意味着什么.
I've read numerous posts about people having problems with viewWillAppear
when you do not create your view hierarchy just right. My problem is I can't figure out what that means.
如果我创建一个 RootViewController
并在该控制器上调用 addSubView
,我希望为 viewWillAppear
事件.
If I create a RootViewController
and call addSubView
on that controller, I would expect the added view(s) to be wired up for viewWillAppear
events.
有没有人有一个复杂的编程视图层次结构的例子,它可以在每个级别成功接收 viewWillAppear
事件?
Does anyone have an example of a complex programmatic view hierarchy that successfully receives viewWillAppear
events at every level?
Apple 的文档状态:
Apple's Docs state:
警告:如果将属于视图控制器的视图直接添加到视图层次结构中,视图控制器将不会收到此消息.如果您在视图层次结构中插入或添加一个视图,并且它有一个视图控制器,您应该直接向关联的视图控制器发送此消息.未能向视图控制器发送此消息将阻止显示任何关联的动画.
问题是他们没有描述如何做到这一点.直接"是什么意思?您如何间接"添加视图?
The problem is that they don't describe how to do this. What does "directly" mean? How do you "indirectly" add a view?
我对 Cocoa 和 iPhone 还是很陌生,所以如果除了基本的 Hello World 废话之外,还有来自 Apple 的有用示例,那就太好了.
I am fairly new to Cocoa and iPhone so it would be nice if there were useful examples from Apple besides the basic Hello World crap.
推荐答案
如果您使用导航控制器并设置其委托,则不会调用视图{Will,Did}{Appear,Disappear} 方法.
If you use a navigation controller and set its delegate, then the view{Will,Did}{Appear,Disappear} methods are not invoked.
您需要改用导航控制器委托方法:
You need to use the navigation controller delegate methods instead:
navigationController:willShowViewController:animated:
navigationController:didShowViewController:animated:
这篇关于iPhone viewWillAppear 不触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!