Note: This article is translated by the ship: thejoeconwayblog
Do you ever feel like this: sometimes some code is put in viewdidload or viewwillappear:, which is confusing. On the stack
In overflow, we often encounter the following problem: which method is called first by viewdidload and viewwillappear? In fact, it is not important to understand the whereabouts of these methods and why they do so. It is important to know the role of these methods and how they coordinate the division of labor with the procedures.
We know that IOS applications generally consist of multiple view controllers, and each View Controller represents the information of a screen. When the program interacts with the user, different view controllers are displayed based on different states in the program. For example, if a user needs to register an account, the screen should display a page that allows the user to enter personal information. After the user completes the registration, the corresponding homepage should be displayed.
Viewwillappear: The method is used to interact with the changed content on the screen. Tell View
The Controller displays the corresponding interface content at the front of the screen: make sure that all text controls in View Controller display the latest information, necessary data is accessible, and appropriate notifications are observed. When viewwillappear: is triggered, the View Controller changes to "active.
Why does the viewdidload method exist? The only reason is that the user interface is sometimes unable or cannot be effectively configured in the XIB file 100%. For example, you sometimes want to set a special attribute for the view (this attribute is unavailable in XIB), and sometimes before setting a background image for a button, you want to modify the image first, for example, when using auto
During layout, you may realize that it is better to use code to write code related to auto layout in XIB.
We cannot infer the status information of the application based on the call of the viewDidLoad method. The viewDidLoad method is actually a simple post-processing mechanism for file loading. In other words, developers should regard the viewDidLoad method as an effective extension of the XIB file, rather than being part of the application running process.
What does this mean? -- We should change the content displayed in the view in the viewWillAppear: method, instead of in viewDidLoad. The content in the view is the data (model object) that the controller transmits to the view during the actual running of the program ).
Here is an example: a bank application that displays the current user balance. When a user clicks a button, a list is displayed in a modal view controller, which lists the ATM machines nearby the user. Here, to obtain the list of nearby ATM machines, the program must use the core location that has been requested by web service. In this case, if the programmer places the operation of requesting the user's ATM list from the server to viewDidLoad. In this case, the view controller is only displayed once (both viewDidLoad and viewWillAppear are called once). Therefore, the server requests the operation of the user's nearby ATM list and places the operation in viewDidLoad and viewWillAppear, the effects on the program are the same.
However, this approach is not good. Think about the situation if you move the ATM view controller to the tab bar controller. Now, the ATM view controller -- the code for obtaining the ATM information in viewDidLoad can only be obtained once. So if you open the program in Atlanta on Tuesday to find the ATM and then check the balance. Next, you went to New York on Wednesday. when you opened the program again, you could only see the ATM information in Atlanta-the view has been loaded, so you will not call the viewDidLoad method.
Note: The program does not exit.
In fact, in this case, when writing code, the biggest error is:
Easiest way is the easiest way. The application changes, and the user interface changes. A simple method at the beginning may cause various problems in the future.
This document consists
Ship breakingTranslation ● reprinted please indicate the source ●