简体   繁体   English

viewDidLoad和viewWillAppear与标签栏控制器

[英]viewDidLoad and viewWillAppear with tab bar controller

I'm looking into the viewDidLoad and viewDidAppear methods to better understand what they both do and I came across an article which uses the example of a banking application to explain how these methods work: 我正在调查viewDidLoad和viewDidAppear方法以更好地理解它们的作用,我发现了一篇文章 ,该文章使用银行应用程序的示例来解释这些方法的工作原理:

Consider a banking application that shows your current balance. 考虑一个显示您当前余额的银行应用程序。 A user can tap on a button, which presents a list of nearby ATMs in a modal view controller. 用户可以点击按钮,该按钮在模态视图控制器中呈现附近的ATM列表。 In order to get the list of nearby ATMs, the application must make a core location and web service request. 为了获得附近ATM的列表,应用程序必须提出核心位置和Web服务请求。

In this situation, a programmer could get away with requesting the list of nearby ATMs from the server in viewDidLoad. 在这种情况下,程序员可以在viewDidLoad中从服务器请求附近的ATM列表。 The view controller is only presented once, both viewDidLoad and viewWillAppear: will be called back to back and only once for that particular view controller instance. 视图控制器只显示一次,viewDidLoad和viewWillAppear:将为该特定视图控制器实例连续调用一次,并且只调用一次。 The net effect of the code in either of these two methods will be the same. 这两种方法中的代码的净效果是相同的。

But this is a bad idea. 但这是个坏主意。 Consider what would happen if you wanted to move the ATM view controller into a tab bar controller. 考虑如果您想将ATM视图控制器移动到标签栏控制器中会发生什么。 Now, the ATM view controller – with its ATM fetching code in viewDidLoad only fetches the list of ATMs once. 现在,ATM视图控制器 - 在viewDidLoad中使用其ATM提取代码仅获取ATM列表一次。 So you are in Atlanta on Tuesday, open up your application to look for an ATM, then check your balance. 因此,您周二在亚特兰大,打开您的申请以寻找自动柜员机,然后检查您的余额。 Then you travel to New York on Wednesday, re-open the banking application, and you only see ATMs in Atlanta. 然后你周三前往纽约,重新打开银行应用程序,你只能看到亚特兰大的ATM。 The view was already loaded, no need to call viewDidLoad and now you're looking at stale data. 视图已加载,无需调用viewDidLoad,现在您正在查看过时的数据。

Sadly, I still don't fully understand how/why both viewDidLoad and viewWillAppear will be called 'back to back', or what adding the ATM view controller to a tab bar controller means in terms of these methods. 遗憾的是,我仍然不完全理解如何/为什么viewDidLoad和viewWillAppear将被“背靠背”调用,或者将ATM视图控制器添加到标签栏控制器的方法就这些方法而言。

viewDidLoad method will call only once a life time of viewController and that is when viewController object will first load in memory. viewDidLoad方法只会调用一次viewController的生命周期,也就是当viewController对象首次加载到内存中时。 where as viewWillAppear method will call every time when a view will appear to screen or you can say will be topViewController... 每当一个视图出现在屏幕上时你会调用viewWillAppear方法,或者你可以说会是topViewController ...

Explanation : Consider you have tab based app with two tabs. 说明 :考虑您有基于选项卡的应用程序和两个选项卡。 Tab1 associated with viewController1 and tab2 is associated with viewController2 . Tab1关联viewController1tab2与相关viewController2 Now when you will run your app and you will see tab one is selected and viewController1 is on view and you want to change to tab2 , when you will tap on tab2 then tabVieController2 's object will create and load to memory first time hence its viewDidLoad method will call, and soon after that it will appear to window and viewWillAppear will also get call. 现在,当您运行应用程序时,您将看到选项卡1被选中并且viewController1处于视图中并且您想要更改为tab2 ,当您点击tab2然后tabVieController2的对象将首次创建并加载到内存,因此它的viewDidLoad方法会调用,很快就会出现在window和viewWillAppear也会调用。 Now if you you try changing tabs by click on them only viewWillAppear methods will get called for both, as they are in memory already. 现在,如果您尝试通过单击更改选项卡,则只会调用viewWillAppear方法,因为它们已经在内存中。

It simple, viewDidLoad get called when the view is load in, either via NIB , storyboard or with the loadView method. 很简单, viewDidLoad在加载视图时被调用,通过NIB ,storyboard或loadView方法。 The viewWillAppear: is called when the view is presented. 在显示视图时调用viewWillAppear: .

When a view is added to a tab bar it only gets load once, thus the viewDidLoad will only be called once. 将视图添加到选项卡栏时,它只会加载一次,因此viewDidLoad只会被调用一次。 But if the user switch to an other tab and back to the same view the viewDidLoad will not be called. 但是,如果用户切换到另一个选项卡并返回到同一视图,则不会调用viewDidLoad This is because the view is already loaded. 这是因为视图已加载。

However the viewWillAppear: is called in both cases just before the view is shown. 但是,在显示视图之前的两种情况下都会调用viewWillAppear: . Thus this will be called when the user first opens the tab and when it switches back to that tab. 因此,当用户首次打开选项卡并切换回该选项卡时,将调用此选项。

I think they are referring to the fact that the view is loaded every time the modal view controller appears (thus the data is constantly refreshed) but only once when it is part of tab bar (only loaded on app launch). 我认为它们指的是每次模态视图控制器出现时加载视图(因此数据不断刷新),但只有当它是标签栏的一部分时才加载(仅在应用程序启动时加载)。 Kind of a whacky example to explain the methods though. 虽然解释方法有点笨拙的例子。

You might want to read up on the view controller lifecycle to know when to implement what in which method: 您可能希望了解视图控制器生命周期,以了解何时实现哪种方法:

Responding to Display-Related Notifications 响应与显示相关的通知

View Loading and Unloading 查看加载和卸载

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM