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

查看:281
本文介绍了-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 those forms.

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

2) 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, becuase 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).

3) ViewDidAppear :最后,我使用ViewDidAppear来启动新线程以执行需要很长时间才能执行的操作,比如执行webservice调用以获取上面表单的额外数据。好处是因为视图已经存在并且正在向用户显示,所以在您获得数据时,可以向用户显示一条漂亮的等待消息。

3) ViewDidAppear: Finally, I use the ViewDidAppear to start off new threads to things that would take a long time to execute, like for example doing a webservice 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天全站免登陆