loadView 与 init 方法

发布于 2024-10-12 09:59:16 字数 213 浏览 4 评论 0原文

请告诉我 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月下凄凉 2024-10-19 09:59:16

你在某些方面是对的:)
当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.

揽清风入怀 2024-10-19 09:59:16

当您访问视图控制器的 view 属性且其值为 nil 时,将调用 loadView

如果视图已被卸载(出于内存目的而调用viewDidUnload),则将再次调用loadView。如果没有,则不会被调用。

您想要的是 viewWillAppear:viewDidAppear:

loadView is called when you access the view property of your view controller and it's nil.

If the view has been unloaded (viewDidUnload has been called for memory purpose) then loadView will be called again. If not it will not be called.

What you want is viewWillAppear: or viewDidAppear:.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文