将视图加载到内存中——调用 viewWillAppear 方法
在视图控制器的生命周期中 viewWillAppear 方法被调用多少次?
我只想在该视图加载到内存中时注册一次通知,并在卸载后取消注册它。
当我执行 viewDidLoad 方法时,我的视图会多次注册。
How many times viewWillAppear method gets called in the lifetime of a view controller?
I wanted to register for a notification only once when that view is loaded in memory and deregister it once it is unloaded.
When I do it viewDidLoad method my view gets register multiple times.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您所要求的正是
-viewDidLoad
和-viewDidUnload
所提供的。 UIViewController 可能会在视图控制器的生命周期内多次加载和卸载其视图(如果视图不可见,则视图控制器会卸载其视图以响应内存警告)。只要在卸载视图时取消注册,您就应该能够在每次加载视图时注册通知。Wht you are asking for is exactly what
-viewDidLoad
and-viewDidUnload
provide. A UIViewController may load and unload it's view many times during the view controller's life (view controllers unload their views in response to memory warnings if the view is not visible). You should be able to register for notifications every time the view is loaded as long as you unregister when the view is unloaded.如果您使用的是 xib 文件,可以尝试使用 awakeFromNib。
You could try using awakeFromNib if you're using a xib file.