loadView 与 init 方法
请告诉我 init 和 loadView 方法何时被调用。 据我所知,init 方法仅在初始化视图时调用一次,并且在加载视图时调用 loadView
。因此,即使您在视图堆栈中推送一个新视图,然后弹出它,也应该调用弹出视图的 loadView
。但是,当我在调试模式下运行代码时,无论我加载同一屏幕多少次,这两个方法都会被调用一次。如果我遗漏了什么,请告诉我。
Please let me know at what times init and loadView
method gets called.
To my knowledge init method gets called only once when view is initialized and loadView
is called anytime view is loaded. So, even if you are pushing a new view in the view stack and then popping it then also the loadView
of the poped up view should get called. But when I am running my code in debugging mode, both of these methods are getting called once, irrespective of how many times I am loading the same screen. Please let me know if I am missing something.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你在某些方面是对的:)
当ViewController对象被实例化时,init方法被调用。每次 ViewController 应将其视图加载到内存时,都会调用 loadView 方法。这可能发生在视图第一次显示之前,或者应该显示第二次、第三次……但之前已从内存中删除时。 (如果您的应用程序内存不足,则可能会发生这种情况。)
如果您想在每次视图可见时执行一些代码,您应该查看方法 viewWillAppear/viewWillDisappear/viewDidAppear/viewDidDisappear。
you are right at some points :)
The init method is being called when the ViewController object is instantiated. The loadView method gets called every time a ViewController should load its view into memory. This can happen before the view is displayed for the first time OR when it should be displayed for a second, third,... time but had been removed from memory before. (this might happen if your app runs out of memory.)
If you want to execute some code every time the view becomes visible, you should have a look at the methods viewWillAppear/viewWillDisappear/viewDidAppear/viewDidDisappear.
当您访问视图控制器的
view
属性且其值为nil
时,将调用loadView
。如果视图已被卸载(出于内存目的而调用
viewDidUnload
),则将再次调用loadView
。如果没有,则不会被调用。您想要的是
viewWillAppear:
或viewDidAppear:
。loadView
is called when you access theview
property of your view controller and it'snil
.If the view has been unloaded (
viewDidUnload
has been called for memory purpose) thenloadView
will be called again. If not it will not be called.What you want is
viewWillAppear:
orviewDidAppear:
.