How to use lazy loading in iOS development UI and ios development ui
1. Basic lazy loading
Lazy loading, also known as delayed loading, is loaded only when needed (low efficiency and low memory usage ). The so-called lazy loading write is its getter method. To put it bluntly, it is to say that in development, when the resources need to be used in the program. Resources are not loaded when the program starts. These resources are loaded only when some resources are required for running.
We know that the memory of iOS devices is limited. If you load all the resources that will be used in the future at one time after the program starts, the memory of iOS devices may be exhausted. These resources, such as a large amount of data, images, audios, and so on, make sure to determine whether there is any lazy loading. If not, instantiate it.
2. Benefits of using lazy loading:
(1) do not write all the code for creating objects in the viewDidLoad method, and the code is more readable.
(2) The getter method of each control is responsible for instantiation, and the code is highly independent and loosely coupled.
(3) only when resources are actually needed can they be loaded, saving memory resources.
3. Sample Code
For example, the application login interface is usually a combination of Lable and textField. We customize an LTView class that includes the titleLable attribute and textField attribute.
Then, we can use the getter method to override the attribute to complete the lazy loading mode. By using lazy loading, We can encapsulate the code according to the module and improve the flexibility of the class, you can also save memory usage for a certain period of time. For the current LTView, lazy loading indicates that I provide two subviews, call the getter method in order to display this subview. If you do not need this subview, LTView is an empty view.
Note: This is what Apple advocates. In fact, many IOS systems made by Apple use lazy loading methods, such as creating a controller View.