Lazy loading of iOS design mode

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: The benefits of lazy loading a: 1. You do not have to write all the code for the object that you created in the Viewdidload method, and the code is more readable.        2. Each property's Getter method is responsible for the respective instantiation, and the code is more independent of each other. 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)            {& nbsp            self.titlelabel = [[UILabel alloc] Initwithframe:cgrectmake (ten, +, +)];&NB Sp            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. Note the problem:   1. Pointer Circular reference problems     IF (!_titlelabel) can not be written as if (!self.titlelabel), otherwise a circular reference pointer will be formed.   return _titlelabel; cannot be written as return self.titlelabel; otherwise a circular reference will be formed. Six. On the code
#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{//is only loaded onto the view that is expected 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];} #pragma mark overrides Titlelabel's Getter Method-(UILabel *) titlelabel{if (!_titlelabel) {Self.titlelabel = [[U]        Ilabel Alloc] Initwithframe:cgrectmake (10, 20, 100, 40)];        Self.titleLabel.textAlignment = Nstextalignmentcenter;        Self.titleLabel.text = @ "What do you look at";        Self.titleLabel.textColor = [Uicolor blackcolor];    [Self.titlelabel SizeToFit]; } return _titlelabel;} #pragma mark rewrites the TextField getter Method-(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 = @ "Look at your drop"; } return _textfield;} @end

Lazy loading of iOS design mode

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.