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 //SLVIEWCONTROLLER.M3 //03-Image Browser Preliminary4 //5 //Created by Apple on 14-5-21.6 //Copyright (c) 2014 itcase. All rights reserved.7 // 8 9 #import "SLViewController.h"Ten One #definePHOTOIMGW 200 A #definePHOTOIMGH 300 - #definePHOTOIMGX 60 - #definePhotoimgy 50 the - @interfaceSlviewcontroller () - - //Variable Declaration +@property (nonatomic, strong) UILabel *Firstlab; -@property (nonatomic, strong) UILabel *Lastlab; +@property (nonatomic, strong) Uiimageview *icon; A@property (nonatomic, strong) UIButton *leftbtn; at@property (nonatomic, strong) UIButton *rightbtn; -@property (nonatomic, strong) Nsarray *Array; -@property (nonatomic, assign)inti; - --(void) Change; - @end in - @implementationSlviewcontroller to +- (void) Viewdidload - { the [Super Viewdidload]; * [self change]; $ }Panax Notoginseng --(void) Change the { + //first get and set A[Self.firstlab settext:[nsstring stringWithFormat:@"%D/5", SELF.I +1]]; the +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); - } the - //Lazy LoadingWuyi /** 1. The image's ordinal label*/ the-(UILabel *) Firstlab - { Wu //determine if there is already, and if not, instantiate - if(!_firstlab) { About_firstlab=[[uilabel Alloc]initwithframe:cgrectmake ( -,Ten, -, -)]; $ [_firstlab Settextalignment:nstextalignmentcenter]; - [Self.view Addsubview:_firstlab]; - } - return_firstlab; A } + the /** 2. Lazy loading of picture controls*/ --(Uiimageview *) icon $ { the //determine if there is already, and if not, instantiate the if(!_icon) { the_icon=[[Uiimageview Alloc]initwithframe:cgrectmake (PHOTOIMGX, Photoimgy, PHOTOIMGW, PHOTOIMGH)]; theUIImage *image=[uiimage imagenamed:@"Biaoqingdi"]; -_icon.image=image; in [Self.view Addsubview:_icon]; the } the return_icon; About } the the /** 3. Describe lazy loading of controls*/ the-(UILabel *) Lastlab + { - //determine if there is already, and if not, instantiate the if(!_lastlab) {Bayi_lastlab=[[uilabel Alloc]initwithframe:cgrectmake ( -, -, -, -)]; the [_lastlab Settextalignment:nstextalignmentcenter]; the [Self.view Addsubview:_lastlab]; - } - return_lastlab; the } the the /** 4. Delay loading of the left-click button*/ the-(UIButton *) leftbtn - { the //determine if there is already, and if not, instantiate the if(!_leftbtn) { the_leftbtn=[UIButton Buttonwithtype:uibuttontypecustom];94_leftbtn.frame=cgrectmake (0, Self.view.center.y, +, +); the[_leftbtn setbackgroundimage:[uiimage imagenamed:@"Left_normal"] forstate:uicontrolstatenormal]; the[_leftbtn setbackgroundimage:[uiimage imagenamed:@"left_highlighted"] forstate:uicontrolstatehighlighted]; the [Self.view addsubview:_leftbtn];98 [_leftbtn addtarget:self Action: @selector (Leftclick:) forcontrolevents:uicontroleventtouchupinside]; About } - return_leftbtn;101 102 }103 104 /** 5. Lazy loading of the right-click button*/ the-(UIButton *) rightbtn106 {107 if(!_rightbtn) {108_rightbtn=[UIButton Buttonwithtype:uibuttontypecustom];109_rightbtn.frame=cgrectmake (photoimgx+photoimgw+Ten, Self.view.center.y, +, +); the[_rightbtn setbackgroundimage:[uiimage imagenamed:@"Right_normal"] forstate:uicontrolstatenormal];111[_rightbtn setbackgroundimage:[uiimage imagenamed:@"right_highlighted"] forstate:uicontrolstatehighlighted]; the [Self.view addsubview:_rightbtn];113 [_rightbtn addtarget:self Action: @selector (RightClick:) forcontrolevents:uicontroleventtouchupinside]; the } the return_rightbtn; the }117 118 //get method for array119-(Nsarray *) Array - {121 if(_array = =Nil) {122NSString *path = [[NSBundle mainbundle] Pathforresource:@"Data"OfType:@"plist"];123_array=[[Nsarray Alloc]initwithcontentsoffile:path];124 } the return_array;126 }127 --(void) RightClick: (UIButton *) btn129 { theself.i++;131 [self change]; the }133 134-(void) Leftclick: (UIButton *) btn135 {136self.i--;137 [self change];138 }139 $ @end
iOS Development UI article-Lazy Loading