懒加载
1.懒加载基本
懒加载——也称为延迟加载,即在需要的时候才加载(效率低,占用内存小)。所谓懒加载,写的是其get方法.
注意:如果是懒加载的话则一定要注意先判断是否已经有了,如果没有那么再去进行实例化
2.使用懒加载的好处:
(1)不必将创建对象的代码全部写在viewdidload方法中,代码的可读性更强
(2)每个控件的getter方法中分别负责各自的实例化处理,代码彼此之间的独立性强,松耦合
3.代码示例
//
// yyviewcontroller.m
// 03-图片浏览器初步
//
// 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
@implementation yyviewcontroller
- (void)viewdidload
{
[super viewdidload];
[self change];
}
-(void)change
{
[self.firstlab settext:[nsstring stringwithformat:@"%d/5",self.i+1]];
//先get再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);
}
//延迟加载
/**1.图片的序号标签*/
-(uilabel *)firstlab
{
//判断是否已经有了,若没有,则进行实例化
if (!_firstlab) {
_firstlab=[[uilabel alloc]initwithframe:cgrectmake(20, 10, 300, 30)];
[_firstlab settextalignment:nstextalignmentcenter];
[self.view addsubview:_firstlab];
}
return _firstlab;
}
/**2.图片控件的延迟加载*/
-(uiimageview *)icon
{
//判断是否已经有了,若没有,则进行实例化
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.描述控件的延迟加载*/
-(uilabel *)lastlab
{
//判断是否已经有了,若没有,则进行实例化
if (!_lastlab) {
_lastlab=[[uilabel alloc]initwithframe:cgrectmake(20, 400, 300, 30)];
[_lastlab settextalignment:nstextalignmentcenter];
[self.view addsubview:_lastlab];
}
return _lastlab;
}
/**4.左键按钮的延迟加载*/
-(uibutton *)leftbtn
{
//判断是否已经有了,若没有,则进行实例化
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.右键按钮的延迟加载*/
-(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;
}
//array的get方法
-(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
xib的简单使用
一、简单介绍
xib和storyboard的比较,一个轻量级一个重量级。
共同点:
都用来描述软件界面
都用interface builder工具来编辑
不同点:
xib是轻量级的,用来描述局部的ui界面
storyboard是重量级的,用来描述整个软件的多个界面,并且能展示多个界面之间的跳转关系
二、xib的简单使用
1.建立xib文件
建立的xib文件命名为appxib.xib
2.对xib进行设置
根据程序的需要,这里把view调整为*布局
建立view模型(设置长宽等参数)
调整布局和内部的控件
完成后的单个view
3.使用xib文件的代码示例
yyviewcontroller.m文件代码如下:
//
// yyviewcontroller.m
// 10-xib文件的使用
//
// 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
@implementation yyviewcontroller
//1.加载数据信息
-(nsarray *)app
{
if (!_app) {
nsstring *path=[[nsbundle mainbundle]pathforresource:@"app.plist" oftype:nil];
nsarray *temparray=[nsarray arraywithcontentsoffile:path];
//字典转模型
nsmutablearray *arraym=[nsmutablearray array ];
for (nsdictionary *dict in temparray) {
[arraym addobject:[yyapp appwithdict:dict]];
}
_app=arraym;
}
return _app;
}
//创建界面原型
- (void)viewdidload
{
[super viewdidload];
nslog(@"%d",self.app.count);
//九宫格布局
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];
//拿出xib视图
nsarray *apparray= [[nsbundle mainbundle]loadnibnamed:@"appxib" owner:nil options:nil];
uiview *appview=[apparray firstobject];
//加载视图
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];
}
}
/**按钮的点击事件*/
-(void)appviewbtnclick:(uibutton *)btn
{
yyapp *apps=self.app[btn.tag];
uilabel *showlab=[[uilabel alloc]initwithframe:cgrectmake(60, 450, 200, 20)];
[showlab settext:[nsstring stringwithformat: @"%@下载成功",apps.name]];
[showlab setbackgroundcolor:[uicolor lightgraycolor]];
[self.view addsubview:showlab];
showlab.alpha=1.0;
//简单的动画效果
[uiview animatewithduration:2.0 animations:^{
showlab.alpha=0;
} completion:^(bool finished) {
[showlab removefromsuperview];
}];
}
@end
运行效果:
三、对xib进行连线示例
1.连线示例
新建一个xib对应的视图类,继承自uiview
在xib界面右上角与新建的视图类进行关联
把xib和视图类进行连线
注意:在使用中把weak改成为强引用。否则...
2.连线后的代码示例
yyviewcontroller.m文件代码如下:
//
// yyviewcontroller.m
// 10-xib文件的使用
//
// 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
@implementation yyviewcontroller
//1.加载数据信息
-(nsarray *)app
{
if (!_app) {
nsstring *path=[[nsbundle mainbundle]pathforresource:@"app.plist" oftype:nil];
nsarray *temparray=[nsarray arraywithcontentsoffile:path];
//字典转模型
nsmutablearray *arraym=[nsmutablearray array ];
for (nsdictionary *dict in temparray) {
[arraym addobject:[yyapp appwithdict:dict]];
}
_app=arraym;
}
return _app;
}
//创建界面原型
- (void)viewdidload
{
[super viewdidload];
nslog(@"%d",self.app.count);
//九宫格布局
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];
//拿出xib视图
nsarray *apparray= [[nsbundle mainbundle]loadnibnamed:@"appxib" owner:nil options:nil];
//注意这里的类型名!
//uiview *appview=[apparray firstobject];
yyappview *appview=[apparray firstobject];
//加载视图
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];
}
}
/**按钮的点击事件*/
-(void)appviewbtnclick:(uibutton *)btn
{
yyapp *apps=self.app[btn.tag];
uilabel *showlab=[[uilabel alloc]initwithframe:cgrectmake(60, 450, 200, 20)];
[showlab settext:[nsstring stringwithformat: @"%@下载成功",apps.name]];
[showlab setbackgroundcolor:[uicolor lightgraycolor]];
[self.view addsubview:showlab];
showlab.alpha=1.0;
//简单的动画效果
[uiview animatewithduration:2.0 animations:^{
showlab.alpha=0;
} completion:^(bool finished) {
[showlab removefromsuperview];
}];
}
@end
yyappview.h文件代码(已经连线)
#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