iOS Development UI article-Lazy Loading

Source: Internet
Author: User

. 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"Ten  One #definePOTOIMGW 200 A #definePOTOIMGH 300 - #definePOTOIMGX 60 - #definePotoimgy 50 the  - @interfaceYyviewcontroller () -  -@property (nonatomic,strong) UILabel *Firstlab; +@property (nonatomic,strong) UILabel *Lastlab; -@property (nonatomic,strong) Uiimageview *icon; +@property (nonatomic,strong) UIButton *leftbtn; A@property (nonatomic,strong) UIButton *rightbtn; at@property (nonatomic,strong) Nsarray *Array; -@property (nonatomic, assign)inti; --(void) Change; - @end -  -  in  - @implementationYyviewcontroller to  +- (void) Viewdidload - { the[Super Viewdidload]; *[self change]; $ }Panax Notoginseng  --(void) Change the { +[Self.firstlab settext:[nsstring stringWithFormat:@"%D/5", self.i+1]]; A     //first get and set 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 /** * The serial number label of the picture*/ 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 /** * . 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 (POTOIMGX, Potoimgy, POTOIMGW, POTOIMGH)]; theUIImage *image=[uiimage imagenamed:@"Biaoqingdi"]; -_icon.image=image; in[Self.view Addsubview:_icon]; the     } the     return_icon; About } the  the /*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 /*delayed 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 /*delayed loading of the right-click button*/ the-(UIButton *) rightbtn106 {107     if(!_rightbtn) {108_rightbtn=[UIButton Buttonwithtype:uibuttontypecustom];109_rightbtn.frame=cgrectmake (potoimgx+potoimgw+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

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.