iOS Development UI article-Lazy Loading

Source: Internet
Author: User

1. Lazy Loading Basic

Lazy Loading--also known as deferred loading--is loaded when needed (low efficiency, small memory footprint). The so-called lazy load, write is its get method.

Note: If it is lazy loading, it is important to be aware of whether there is already, if not then to instantiate

2. Benefits of using lazy loading:

(1) You do not have to write all the code of the created object in the Viewdidload method, the code is more readable

(2) Each control's getter method is responsible for the respective instantiation processing, the code is independent of each other, loosely coupled

3. Code examples

  1//2//YYVIEWCONTROLLER.M 3//03-Image Browser Preliminary 4//5//Created by Apple on 14-5-21. 6//Copyright (c) 2014 itcase.  All rights reserved.    7//8 9 #import "YYViewController.h" #define POTOIMGW #define POTOIMGH #define POTOIMGX #define POTOIMGY @interface Yyviewcontroller () @property (Nonatomic,strong) UILabel *firstlab; @property (Nonatomic,strong) UILabel *lastlab; @property (Nonatomic,strong) Uiimageview *icon; @property (Nonatomic,strong) UIButton *leftbtn; @property (Nonatomic,strong) UIButton *rightbtn; @property (Nonatomic,strong) Nsarray *array; @property (nonatomic, assign) int i; -(void) change;     @end @implementation Yyviewcontroller-(void) viewdidload {[Super viewdidload]; 35 [Self change]; [Self.firstlab settext:[nsstring stringwithformat:@ "%D/5", Self.i+1]]; 41//First Get and then Set Self.icon.image=[uiimage imagenamed:self.array[self.i][@ "Name"]; self.lastlab.text=self.array[self.i][@ "desc"]; Self.leftbtn.enabled= (self.i!=0); Self.rightbtn.enabled= (self.i!=4);          48} 49 50//Delayed Load 51/**1. The sequence number label of the picture */-(UILabel *) Firstlab 53 {54//To determine if there is already, if not, then instantiate if (!_firstlab) {56 _firstlab=[[uilabel Alloc]initwithframe:cgrectmake (20, 10, 300, 30)]; [_firstlab Settextalignment:nstextalignmentcenter]; [Self.view Addsubview:_firstlab]; _firstlab return; 61} 62 63/**2. Delay loading of the picture control */-(Uiimageview *) icon 65 {66//determine if it already exists, if not, then instantiate if (!_icon) {_i Con=[[uiimageview Alloc]initwithframe:cgrectmake (POTOIMGX, Potoimgy, POTOIMGW, POTOIMGH)]; UIImage *image=[uiimage imagenamed:@ "Biaoqingdi"]; _icon.image=image; [Self.view Addsubview:_icon]; _icon return; 74} 75 76/**3. Description of the control's lazy load */UILabel-(*) Lastlab 78 {79//To determine if there is already, if not, then instantiate if (!_laStlab) {Bayi _lastlab=[[uilabel Alloc]initwithframe:cgrectmake (+, +, +)]; [_lastlab Settextalig Nment:nstextalignmentcenter]; [Self.view Addsubview:_lastlab]; +------return _lastlab;         86} 87 88/**4. Delay loading of the left button */UIButton-(*) LEFTBTN 90 {91//To determine if there is already, if not, then instantiate if (!_LEFTBTN) {93 _leftbtn=[uibutton Buttonwithtype:uibuttontypecustom]; 94 _leftbtn.frame=cgrectmake (0, Self.view.center.y, 40, 40); [_leftbtn setbackgroundimage:[uiimage imagenamed:@ "Left_normal"] forstate:uicontrolstatenormal]; [_leftbtn setbackgroundimage:[uiimage imagenamed:@ "left_highlighted"] forstate:uicontrolstatehighlighted]; [Self.view ADDSUBVIEW:_LEFTBTN]; 98 [_leftbtn addtarget:self Action: @selector (Leftclick:) forcontrolevents:uicontroleventtouchupinside]; 99}10         0 return _leftbtn;101 102}103 104/**5. Delay loading of the right button */105-(UIButton *) rightbtn106 {107 if (!_RIGHTBTN) {108 _rightbtn=[UIButton buttonwithtype:uibuttontypecustom];109 _rightbtn.frame=cgrectmake (potoimgx+potoimgw+10, Self.view.cente R.Y, [_rightbtn setbackgroundimage:[uiimage imagenamed:@ "Right_normal"] forstate:uicontrolstatenormal] ; 111 [_rightbtn setbackgroundimage:[uiimage imagenamed:@ "right_highlighted"] forstate:uicontrolstatehighlighted];1 [Self.view addsubview:_rightbtn];113 [_rightbtn addtarget:self Action: @selector (rightclick:) Forcontrol events:uicontroleventtouchupinside];114}115 return _rightbtn;116}117 118//array Get Method 119-(Nsarray *) array120 {         121 if (_array==nil) {122 nsstring *path=[[nsbundle Mainbundle] pathforresource:@ "Data" oftype:@ "plist"];123 _array=[[nsarray alloc]initwithcontentsoffile:path];124}125 return _array;126}127-(void) RightClick: ( UIButton *) btn129 {self.i++;131 [self change];132}133 134-(void) Leftclick: (UIButton *) btn135 {136 SELF.I --;137 [self change];138}139 @end 

iOS Development UI article-lazy loading (GO)

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.