ios-design mode-Lazy loading

Source: Internet
Author: User

one. Why lazy loading? A: The memory of the iphone device is limited, and if it loads all of the resources that will be used in the future after the program starts, it may run out of memory for the iOS device. These resources such as lots of data, pictures, audio, too many controls, etc.two. What is lazy loading thought? A: The simple will is to drag to the last moment, a necessity, before loading, only to start to occupy resources. The so-called lazy loading, write is its getter method. In development, when the program needs to use the resources, when the program starts to not load, only when the run when the resources needed to load these resources.Three: Lazy loading of goodOffice A: 1. You do not have to write the code that creates the object in the Viewdidload method, and the code is more readable.        2. Each property of the getter method is responsible for the pigeon instantiation, respectively, the code between each other more independent. 3. Save memory resources Only when resources are really needed and then loaded.Four: Step:1. Declare the required attribute in the @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 that declares the property (remember not to write it yourself, Xcode will automatically prompt it, write it down) #pragma mark rewrite Titlelabel's Getter method
-(UILabel *) Titlelabel
      {          if (!_titlelabel)           {             self.titlelabel = [[UILabel alloc] Initwithframe:cgrectmake (ten, +, +)];&N Bsp            self.titlelabel.textalignment = nstextalignmentcenter;      & nbsp      self.titlelabel.textcolor = [Uicolor blackcolor];             self . Titlelabel.text = @ "hehe";             [self.titlelabel sizetofit];         }        return _titlelabel;     }     3. Calling the overridden method &n Bsp;self. Method name      //Call the Initui method inside the viewdidload.      -(void) initui     {         //is only loaded onto the view that you expect to load. It is not alloc/init in the initialization method. Just a pointer that does not account for memory.           [Self.view addsubview:self.titlelabel];          [Self.view addsubview:self.textfield];     } Five. Issues of attention: 1. The pointer circular reference problem if (!_titlelabel) can not be written as if (!self.titlelabel), or it will form a circular reference pointer. return _titlelabel; cannot be written as return self.titlelabel; otherwise a circular reference will be formed.Six. On the code
#import "ViewController.h"@interfaceViewcontroller () @property (nonatomic,strong) UILabel*Titlelabel, @property (nonatomic,strong) Uitextfield*TextField;@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; [Self initui];}- (void) initui{//just load onto the view that you expect to load. It is not alloc/init in the initialization method. Just a pointer that does not account for memory. [Self.view AddSubview:self.titleLabel]; [Self.view AddSubview:self.textField];}#pragmaMark rewrites Titlelabel's Getter Method-(UILabel *) titlelabel{if(!_titlelabel) {Self.titlelabel= [[UILabel alloc] Initwithframe:cgrectmake (Ten, -, -, +)]; Self.titleLabel.textAlignment=Nstextalignmentcenter; Self.titleLabel.text=@"hehe"; Self.titleLabel.textColor=[Uicolor Blackcolor];    [Self.titlelabel SizeToFit]; }        return_titlelabel;}#pragmaMark's rewrite of the TextField getter Method-(Uitextfield *) textfield{if(!_textfield) {Self.textfield= [[Uitextfield alloc] Initwithframe:cgrectmake (Ten, -, -, +)]; Self.textField.borderStyle=Uitextborderstyleroundedrect; Self.textField.clearButtonMode=uitextfieldviewmodewhileediting; Self.textField.placeholder=@"hehe"; }    return_textfield;}@end

ios-design mode-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.