ios UIViewController viewDidLoad vs. viewWillAppear:正确的分工是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1579550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UIViewController viewDidLoad vs. viewWillAppear: What is the proper division of labor?
提问by dugla
I have always been a bit unclear on the type of tasks that should be assigned to viewDidLoad
vs. viewWillAppear
: in a UIViewController
subclass.
对于应该分配给viewDidLoad
vs.的任务类型,我一直有点不清楚viewWillAppear
:在UIViewController
子类中。
e.g. I am doing an app where I have a UIViewController
subclass hitting a server, getting data, feeding it to a view and then displaying that view. What are the pros and cons of doing this in viewDidLoad
vs. viewWillAppear
?
例如,我正在做一个应用程序,我有一个UIViewController
子类访问服务器,获取数据,将其提供给视图,然后显示该视图。在viewDidLoad
vs.中这样做的利弊是什么viewWillAppear
?
回答by LeonBrussels
viewDidLoad is things you have to do once. viewWillAppear gets called every time the view appears. You should do things that you only have to do once in viewDidLoad - like setting your UILabel texts. However, you may want to modify a specific part of the view every time the user gets to view it, e.g. the iPod application scrolls the lyrics back to the top every time you go to the "Now Playing" view.
viewDidLoad 是你必须做一次的事情。每次出现视图时都会调用 viewWillAppear。你应该做一些你只需要在 viewDidLoad 中做一次的事情——比如设置你的 UILabel 文本。但是,您可能希望在用户每次查看时修改视图的特定部分,例如,每次进入“正在播放”视图时,iPod 应用程序都会将歌词滚动回顶部。
However, when you are loading things from a server, you also have to think about latency. If you pack all of your network communication into viewDidLoad or viewWillAppear, they will be executed before the user gets to see the view - possibly resulting a short freeze of your app. It may be good idea to first show the user an unpopulated view with an activity indicator of some sort. When you are done with your networking, which may take a second or two (or may even fail - who knows?), you can populate the view with your data. Good examples on how this could be done can be seen in various twitter clients. For example, when you view the author detail page in Twitterrific, the view only says "Loading..." until the network queries have completed.
但是,当您从服务器加载内容时,您还必须考虑延迟。如果您将所有网络通信打包到 viewDidLoad 或 viewWillAppear 中,它们将在用户看到视图之前执行 - 可能导致您的应用程序短暂冻结。首先向用户显示带有某种活动指示器的未填充视图可能是个好主意。当您完成网络连接时,这可能需要一两秒钟(甚至可能失败 - 谁知道?),您可以使用您的数据填充视图。可以在各种 twitter 客户端中看到如何做到这一点的好例子。例如,当您在 Twitterrific 中查看作者详细信息页面时,该视图只会显示“正在加载...”,直到网络查询完成。
回答by Jaminyah
Initially used only ViewDidLoad with tableView. On testing with loss of Wifi, by setting device to airplane mode, realized that the table did not refresh with return of Wifi. In fact, there appears to be no way to refresh tableView on the device even by hitting the home button with background mode set to YES in -Info.plist.
最初仅将 ViewDidLoad 与 tableView 一起使用。在 Wifi 丢失的情况下进行测试,通过将设备设置为飞行模式,发现表没有随着 Wifi 的返回而刷新。事实上,即使在 -Info.plist 中将背景模式设置为 YES 的情况下点击主页按钮,似乎也无法刷新设备上的 tableView。
My solution:
我的解决方案:
-(void) viewWillAppear: (BOOL) animated { [self.tableView reloadData];}
回答by keisar
It's important to note that using viewDidLoad for positioning is a bit risky and should be avoided since the bounds are not set. this may cause unexpected results (I had a variety of issues...)
需要注意的是,使用 viewDidLoad 进行定位有点冒险,应该避免,因为没有设置边界。这可能会导致意想不到的结果(我有各种各样的问题......)
This postdescribes quite well the different methods and what happens in each of them.
这篇文章很好地描述了不同的方法以及每种方法中发生的情况。
currently for one-time init and positioning I'm thinking of using viewDidAppear with a flag, if anyone has any other recommendation please let me know.
目前对于一次性初始化和定位,我正在考虑使用带有标志的 viewDidAppear,如果有人有任何其他建议,请告诉我。
回答by Honey
Depends, Do you need the data to be loaded eachtime you open the view? or only once?
取决于,每次打开视图时都需要加载数据吗?还是只有一次?
- Red :They don't require to change every time. Once they are loaded they stay as how they were.
- Purple:They need to change over time or after you load each time. You don't want to see the same 3 suggested users to follow, it needs to be reloaded every time you come back to the screen. Their photos may get updated... you don't want to see a photo from 5 years ago...
- 红色:它们不需要每次都改变。一旦它们被加载,它们就会保持原样。
- 紫色:它们需要随着时间的推移或每次加载后改变。您不希望看到相同的 3 个建议用户关注,每次返回屏幕时都需要重新加载。他们的照片可能会更新……你不想看到 5 年前的照片……
viewDidLoad:
Whatever processing you have that needs to be done once.viewWilLAppear:
Whatever processing that needs to change every time the page is loaded.
viewDidLoad:
无论您有什么处理,都需要完成一次。viewWilLAppear:
每次加载页面时需要更改的任何处理。
Labels, icons, button titles or most dataInputedByDeveloper usuallydon't change. Names, photos, links, button status, lists (input Arrays for your tableViews or collectionView) or most dataInputedByUser usuallydo change.
标签、图标、按钮标题或大多数 dataInputedByDeveloper通常不会改变。名称、照片、链接、按钮状态、列表(tableViews 或 collectionView 的输入数组)或大多数 dataInputedByUser通常会更改。