IOS-design mode-lazy loading, ios-design mode loading

Source: Internet
Author: User

IOS-design mode-lazy loading, ios-design mode loading
I. Why do I need to load it lazily? A: the memory of the iPhone device 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 the iOS device may be exhausted for a long time. These resources include a large amount of data, images, audio, and excessive controls. Ii. What is the concept of lazy loading? A: Simply put, it is to wait until the last moment until it is loaded to occupy resources. The so-called lazy loading write is its getter method. During development, resources to be used in a program are not loaded when the program starts. These resources are loaded only when these resources are required during running. Iii. Benefits of lazy loading: 1. Do not write all the code for creating objects in the viewDidLoad method, so the code is more readable. 2. The getter method of each attribute is responsible for the instantiation of pigeons, and the code is more independent from each other. 3. Only when resources are actually needed can they be loaded, saving memory resources. Step 4: 1. Declare the required attribute in @ interface. For example, I need two controls: label and textfield @ interface ViewController () @ property (nonatomic, strong) UILabel * titleLabel; @ property (nonatomic, strong) UITextField * textField; @ end 2. override the getter method of declared attributes (remember not to manually write it by yourself. Xcode will automatically prompt you to write it out and it will be charged) # pragma mark will rewrite the getter method of titleLabel
-(UILabel *) titleLabel
{If (! _ TitleLabel) {self. titleLabel = [[UILabel alloc] initWithFrame: CGRectMake (10, 20,100, 40)]; self. titleLabel. textAlignment = NSTextAlignmentCenter; self. titleLabel. textColor = [UIColor blackColor]; self. titleLabel. text = @ "Haha"; [self. titleLabel sizeToFit];} return _ titleLabel;} 3. call the override method self. method Name // call the initUI method in viewDidLoad. -(Void) initUI {// only load to the expected view. It is not in the initialization method for its alloc/init. It is just a pointer and does not occupy memory. [Self. view addSubview: self. titleLabel]; [self. view addSubview: self. textField];} 5. Notes: 1. pointer loop reference problem if (! _ TitleLabel) cannot be written as if (! Self. titleLabel), otherwise it will form a circular reference pointer. Return _ titleLabel; return self. titleLabel cannot be written; otherwise, a circular reference is formed. 2. If you use a vfl statement or the encapsulated Masonry layout page, the code about the frame cannot be written in the rewritten get method. Otherwise, an error indicating that the parent view cannot be found will be reported. Put it behind addSubView, for example: [self. view addSubview: self. textField]; 6. Code on

# Import "ViewController. h "@ interface ViewController () @ property (nonatomic, strong) UILabel * titleLabel; @ property (nonatomic, strong) UITextField * textField; @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; [self initUI];}-(void) initUI {// load only to the expected view. It is not in the initialization method for its alloc/init. It is just a pointer and does not occupy memory. [Self. view addSubview: self. titleLabel]; [self. view addSubview: self. textField] ;}# pragma mark override the getter method of titleLabel-(UILabel *) titleLabel {if (! _ TitleLabel) {self. titleLabel = [[UILabel alloc] initWithFrame: CGRectMake (10, 20,100, 40)]; self. titleLabel. textAlignment = NSTextAlignmentCenter; self. titleLabel. text = @ "Haha"; self. titleLabel. textColor = [UIColor blackColor]; [self. titleLabel sizeToFit];} return _ titleLabel;} # getter method of textField rewritten by pragma mark-(UITextField *) textField {if (! _ TextField) {self. textField = [[UITextField alloc] initWithFrame: CGRectMake (10,100,100, 40)]; self. textField. borderStyle = UITextBorderStyleRoundedRect; self. textField. clearButtonMode = UITextFieldViewModeWhileEditing; self. textField. placeholder = @ "hehe";} return _ textField;} @ end

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.