A simple way to use lazy loading and xib in UI development for IOS applications _ios

Source: Internet
Author: User
Tags reserved uikit

Lazy load

1. Lazy Load Basic

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

Note: If it is lazy to load the words must be careful to determine whether there is already, if not then to instantiate

2. The advantages of using lazy loading:

(1) Do not have to write all the code to create the object in the Viewdidload method, the code is more readable

(2) Each control's getter method is responsible for the respective instantiation process, the code is independent of each other strong, loose coupling

3. code example

Copy Code code as follows:

//
Yyviewcontroller.m
03-Picture Browser Preliminary
//
Created by Apple on 14-5-21.
Copyright (c) 2014 itcase. All rights reserved.
//

#import "YYViewController.h"

#define POTOIMGW 200
#define POTOIMGH 300
#define POTOIMGX 60
#define POTOIMGY 50

@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


Copy Code code as follows:

@implementation Yyviewcontroller

-(void) viewdidload
{
[Super Viewdidload];
[Self change];
}

-(void) Change
{
[Self.firstlab settext:[nsstring stringwithformat:@ "%D/5", self.i+1]];
First Get and 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);
}

//deferred loading
/**1. Picture's serial number label */
-(Uilabel *) Firstlab
{
   //Determine if there is already, if not, instantiate
     if (!_firstlab) {
        _firstlab=[[uilabel alloc] Initwithframe:cgrectmake (20, 10, 300, 30)];
        [_firstlab Settextalignment:nstextalignmentcenter];
        [Self.view Addsubview:_firstlab];
   }
    return _firstlab;
}

/**2. Deferred loading */
for picture Controls-(Uiimageview *) icon
{
    //To determine if there is already, if not, instantiate
     if (!_icon) {
        _icon=[[uiimageview alloc]initwithframe: CGRectMake (POTOIMGX, Potoimgy, POTOIMGW, POTOIMGH)];
        uiimage *image=[uiimage imagenamed:@ "Biaoqingdi"];
        _icon.image=image;
        [Self.view Addsubview:_icon];
   }
    return _icon;
}

/**3. Describe the control's deferred loading */
-(Uilabel *) Lastlab
{
    //To determine if it is already there, and if not, instantiate it
     if (!_lastlab) {
        _lastlab=[[uilabel alloc]initwithframe: CGRectMake (20, 400, 300, 30)];
        [_lastlab Settextalignment:nstextalignmentcenter];
        [Self.view Addsubview:_lastlab];
   }
    return _lastlab;
}

/**4. Delayed loading of Left-button buttons * *
-(UIButton *) leftbtn
{
Determine if there is already, if not, then instantiate
if (!_LEFTBTN) {
_leftbtn=[uibutton Buttonwithtype:uibuttontypecustom];
_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];
[_leftbtn addtarget:self Action: @selector (Leftclick:) forcontrolevents:uicontroleventtouchupinside];
}
return _leftbtn;

}

/**5. Deferred loading */
of the right button (UIButton *) rightbtn
{
    if (!_rightbtn) {
         _rightbtn=[uibutton Buttonwithtype:uibuttontypecustom];
        _rightbtn.frame=cgrectmake (potoimgx+potoimgw+10, Self.view.center.y, 40, 40);
        [_rightbtn setbackgroundimage:[uiimage imagenamed:@ "Right_normal"] Forstate:uicontrolstatenormal];
        [_rightbtn setbackgroundimage:[uiimage imagenamed:@] Right_ Highlighted "] forstate:uicontrolstatehighlighted];
        [Self.view addsubview:_rightbtn];
        [_rightbtn addtarget:self Action: @selector (RightClick:) Forcontrolevents:uicontroleventtouchupinside];
   }
    return _rightbtn;
}

The Get method of array
-(Nsarray *) array
{
if (_array==nil) {
NSString *path=[[nsbundle Mainbundle] pathforresource:@ "Data" oftype:@ "plist"];
_array=[[nsarray Alloc]initwithcontentsoffile:path];
}
return _array;
}

-(void) RightClick: (UIButton *) btn
{
self.i++;
[Self change];
}

-(void) Leftclick: (UIButton *) btn
{
self.i--;
[Self change];
}

@end


simple use of xib
First, a brief introduction

Comparison of Xib and storyboard, a lightweight one heavyweight.

Common:

is used to describe the software interface.

are edited using the Interface Builder tool.

Different points:

Xib is lightweight and is used to describe the local UI interface

Storyboard is a heavyweight, used to describe multiple interfaces of the entire software, and to show the jump relationships between multiple interfaces

Second, the simple use of xib

1. Establishment of XIB documents

The established Xib file is named Appxib.xib

2. Set the Xib

Adjust view to free layout as required by the program

Create a view model (set parameters such as long width)

Adjust the layout and internal controls

Single View after completion

3. code example using the Xib file

The yyviewcontroller.m file code is as follows:

Copy Code code as follows:

//
Yyviewcontroller.m
Use of 10-xib files
//
Created by Apple on 14-5-24.
Copyright (c) 2014 itcase. All rights reserved.
//

#import "YYViewController.h"
#import "YYapp.h"

@interface Yyviewcontroller ()
@property (Nonatomic,strong) Nsarray *app;
@end


Copy Code code as follows:

@implementation Yyviewcontroller

1. Loading Data information
-(Nsarray *) app
{
if (!_app) {
NSString *path=[[nsbundle mainbundle]pathforresource:@ "App.plist" oftype:nil];
Nsarray *temparray=[nsarray Arraywithcontentsoffile:path];

Dictionary Turn model
Nsmutablearray *arraym=[nsmutablearray Array];
For (Nsdictionary *dict in Temparray) {
[Arraym Addobject:[yyapp Appwithdict:dict]];
}
_app=arraym;
}
return _app;
}

Creating an interface prototype
-(void) viewdidload
{
[Super Viewdidload];
NSLog (@ "%d", self.app.count);

Nine Sudoku layout
int totalloc=3;
CGFloat appvieww=80;
CGFloat appviewh=90;
CGFloat margin= (SELF.VIEW.FRAME.SIZE.WIDTH-TOTALLOC*APPVIEWW)/(totalloc+1);

int count=self.app.count;
for (int i=0; i<count; i++) {

int row=i/totalloc;
int loc=i%totalloc;
CGFloat Appviewx=margin + (margin +appvieww) *loc;
CGFloat Appviewy=margin + (margin +appviewh) *row;
Yyapp *app=self.app[i];

Take out the Xib view
Nsarray *apparray= [[NSBundle mainbundle]loadnibnamed:@ "Appxib" Owner:nil Options:nil];
UIView *appview=[apparray Firstobject];
Load view
Appview.frame=cgrectmake (Appviewx, Appviewy, APPVIEWW, APPVIEWH);

Uiimageview *appviewimg= (Uiimageview *) [Appview viewwithtag:1];
Appviewimg.image=app.image;

Uilabel *appviewlab= (Uilabel *) [Appview viewwithtag:2];
Appviewlab.text=app.name;

UIButton *appviewbtn= (UIButton *) [Appview Viewwithtag:3];
[Appviewbtn addtarget:self Action: @selector (Appviewbtnclick:) forcontrolevents:uicontroleventtouchupinside];
Appviewbtn.tag=i;

[Self.view Addsubview:appview];
}
}

/** button's Click event * *
-(void) Appviewbtnclick: (UIButton *) btn
{
Yyapp *apps=self.app[btn.tag];
Uilabel *showlab=[[uilabel Alloc]initwithframe:cgrectmake (60, 450, 200, 20)];
[Showlab settext:[nsstring stringWithFormat: @ "%@ download Successful", Apps.name]];
[Showlab Setbackgroundcolor:[uicolor Lightgraycolor]];
[Self.view Addsubview:showlab];
showlab.alpha=1.0;

Simple animation effect
[UIView animatewithduration:2.0 animations:^{
showlab.alpha=0;
} completion:^ (BOOL finished) {
[Showlab Removefromsuperview];
}];
}

@end


Operation Effect:

Third, connect to the Xib sample

1. Wiring Example

Creates a new xib corresponding view class that inherits from UIView

To associate with a newly created view class in the upper-right corner of the Xib interface

Connect the xib with the view class

Note: Change the weak into a strong reference in use. Otherwise...

2. code example after wiring

The yyviewcontroller.m file code is as follows:

Copy Code code as follows:

//
Yyviewcontroller.m
Use of 10-xib files
//
Created by Apple on 14-5-24.
Copyright (c) 2014 itcase. All rights reserved.
//

#import "YYViewController.h"
#import "YYapp.h"
#import "YYappview.h"

@interface Yyviewcontroller ()
@property (Nonatomic,strong) Nsarray *app;
@end


Copy Code code as follows:

@implementation Yyviewcontroller

1. Loading Data information
-(Nsarray *) app
{
if (!_app) {
NSString *path=[[nsbundle mainbundle]pathforresource:@ "App.plist" oftype:nil];
Nsarray *temparray=[nsarray Arraywithcontentsoffile:path];

Dictionary Turn model
Nsmutablearray *arraym=[nsmutablearray Array];
For (Nsdictionary *dict in Temparray) {
[Arraym Addobject:[yyapp Appwithdict:dict]];
}
_app=arraym;
}
return _app;
}

Creating an interface prototype
-(void) viewdidload
{
[Super Viewdidload];
NSLog (@ "%d", self.app.count);

Nine Sudoku layout
int totalloc=3;
CGFloat appvieww=80;
CGFloat appviewh=90;
CGFloat margin= (SELF.VIEW.FRAME.SIZE.WIDTH-TOTALLOC*APPVIEWW)/(totalloc+1);

int count=self.app.count;
for (int i=0; i<count; i++) {

int row=i/totalloc;
int loc=i%totalloc;
CGFloat Appviewx=margin + (margin +appvieww) *loc;
CGFloat Appviewy=margin + (margin +appviewh) *row;
Yyapp *app=self.app[i];

Take out the Xib view
Nsarray *apparray= [[NSBundle mainbundle]loadnibnamed:@ "Appxib" Owner:nil Options:nil];

Note the type name here!
UIView *appview=[apparray Firstobject];
Yyappview *appview=[apparray Firstobject];

Load view
Appview.frame=cgrectmake (Appviewx, Appviewy, APPVIEWW, APPVIEWH);
[Self.view Addsubview:appview];

Appview.appimg.image=app.image;
Appview.applab.text=app.name;
Appview.appbtn.tag=i;

[Appview.appbtn addtarget:self Action: @selector (Appviewbtnclick:) forcontrolevents:uicontroleventtouchupinside];

}
}

/** button's Click event * *
-(void) Appviewbtnclick: (UIButton *) btn
{
Yyapp *apps=self.app[btn.tag];
Uilabel *showlab=[[uilabel Alloc]initwithframe:cgrectmake (60, 450, 200, 20)];
[Showlab settext:[nsstring stringWithFormat: @ "%@ download Successful", Apps.name]];
[Showlab Setbackgroundcolor:[uicolor Lightgraycolor]];
[Self.view Addsubview:showlab];
showlab.alpha=1.0;

Simple animation effect
[UIView animatewithduration:2.0 animations:^{
showlab.alpha=0;
} completion:^ (BOOL finished) {
[Showlab Removefromsuperview];
}];
}

@end


YYappview.h file code (already wired)
Copy Code code as follows:

#import <UIKit/UIKit.h>

@interface Yyappview:uiview
@property (Strong, nonatomic) Iboutlet Uiimageview *appimg;
@property (Strong, nonatomic) Iboutlet Uilabel *applab;
@property (Strong, nonatomic) Iboutlet UIButton *appbtn;
@end

Related Article

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.