-viewWillAppear: 和 -viewDidAppear: 有什么区别? [英] What is the difference between -viewWillAppear: and -viewDidAppear:?

查看:19
本文介绍了-viewWillAppear: 和 -viewDidAppear: 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

-[UIViewController viewWillAppear:]-[UIViewController viewDidAppear:] 有什么区别?

推荐答案

总的来说,我是这样做的:

In general, this is what I do:

  1. ViewDidLoad - 每当我将控件添加到应该与视图一起出现的视图时,我都会立即将其放入 ViewDidLoad 方法中.基本上,只要将视图加载到内存中,就会调用此方法.例如,如果我的视图是一个带有 3 个标签的表单,我会在此处添加标签;没有这些形式,视图将永远存在.

  1. ViewDidLoad - Whenever I'm adding controls to a view that should appear together with the view, right away, I put it in the ViewDidLoad method. Basically, this method is called whenever the view was loaded into memory. So for example, if my view is a form with 3 labels, I would add the labels here; the view will never exist without these forms.

ViewWillAppear:我使用 ViewWillAppear 通常只是为了更新表单上的数据.因此,对于上面的示例,我将使用它来实际将我的域中的数据加载到表单中.创建 UIViews 是相当昂贵的,你应该尽可能避免在 ViewWillAppear 方法上这样做,因为当它被调用时,这意味着 iPhone 已经准备好向用户显示 UIView,您在此处执行的任何繁重操作都会以非常明显的方式影响性能(例如动画被延迟等).

ViewWillAppear: I use ViewWillAppear usually just to update the data on the form. So, for the example above, I would use this to actually load the data from my domain into the form. Creation of UIViews is fairly expensive, and you should avoid as much as possible doing that on the ViewWillAppear method because when this gets called, it means that the iPhone is already ready to show the UIView to the user, and anything heavy you do here will impact performance in a very visible manner (like animations being delayed, etc).

ViewDidAppear:最后,我使用 ViewDidAppear 来启动新线程来处理需要很长时间执行的事情,例如做一个Web 服务调用以获取上述表单的额外数据.好消息是,因为视图已经存在并且正在向用户显示,所以您可以显示一个很好的等待"视图.获取数据时向用户发送消息.

ViewDidAppear: Finally, I'm using ViewDidAppear to start off new threads to things that would take a long time to execute, like for example doing a web service call to get extra data for the form above. The good thing is that because the view already exists and is being displayed to the user, you can show a nice "Waiting" message to the user while you get the data.

这篇关于-viewWillAppear: 和 -viewDidAppear: 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆