49
50introw=i/totalloc;
51intloc=i%totalloc;
52CGFloatappviewX=margin+(margin+appviewW)*loc;
53CGFloatappviewY=margin+(margin+appviewH)*row;
54YYapp*app=self.app[i];
55
56//拿出xib视图
57NSArray*apparray=[[NSBundlemainBundle]loadNibNamed:
@"appxib"owner:
niloptions:
nil];
58UIView*appview=[apparrayfirstObject];
59//加载视图
60appview.frame=CGRectMake(appviewX,appviewY,appviewW,appviewH);
61
62UIImageView*appviewImg=(UIImageView*)[appviewviewWithTag:
1];
63appviewImg.image=app.image;
64
65UILabel*appviewlab=(UILabel*)[appviewviewWithTag:
2];
66appviewlab.text=app.name;
67
68UIButton*appviewbtn=(UIButton*)[appviewviewWithTag:
3];
69[appviewbtnaddTarget:
selfaction:
@selector(appviewbtnClick:
)forControlEvents:
UIControlEventTouchUpInside];
70appviewbtn.tag=i;
71
72[self.viewaddSubview:
appview];
73}
74}
75
76/**按钮的点击事件*/
77-(void)appviewbtnClick:
(UIButton*)btn
78{
79YYapp*apps=self.app[btn.tag];
80UILabel*showlab=[[UILabelalloc]initWithFrame:
CGRectMake(60,450,200,20)];
81[showlabsetText:
[NSStringstringWithFormat:
@"%@下载成功",apps.name]];
82[showlabsetBackgroundColor:
[UIColorlightGrayColor]];
83[self.viewaddSubview:
showlab];
84showlab.alpha=1.0;
85
86//简单的动画效果
87[UIViewanimateWithDuration:
2.0animations:
^{
88showlab.alpha=0;
89}completion:
^(BOOLfinished){
90[showlabremoveFromSuperview];
91}];
92}
93
94@end
运行效果:
三、对xib进行连线示例
1.连线示例
新建一个xib对应的视图类,继承自Uiview
在xib界面右上角与新建的视图类进行关联
把xib和视图类进行连线
注意:
在使用中把weak改成为强引用。
否则...
2.连线后的代码示例
YYViewController.m文件代码如下:
1//
2//YYViewController.m
3//10-xib文件的使用
4//
5//Createdbyappleon14-5-24.
6//Copyright(c)2014年itcase.Allrightsreserved.
7//
8
9#import"YYViewController.h"
10#import"YYapp.h"
11#import"YYappview.h"
12
13@interfaceYYViewController()
14@property(nonatomic,strong)NSArray*app;
15@end
16
17@implementationYYViewController
18
19//1.加载数据信息
20-(NSArray*)app
21{
22if(!
_app){
23NSString*path=[[NSBundlemainBundle]pathForResource:
@"app.plist"ofType:
nil];
24NSArray*temparray=[NSArrayarrayWithContentsOfFile:
path];
25
26//字典转模型
27NSMutableArray*arrayM=[NSMutableArrayarray];
28for(NSDictionary*dictintemparray){
29[arrayMaddObject:
[YYappappWithDict:
dict]];
30}
31_app=arrayM;
32}
33return_app;
34}
35
36//创建界面原型
37-(void)viewDidLoad
38{
39[superviewDidLoad];
40NSLog(@"%d",self.app.count);
41
42//九宫格布局
43inttotalloc=3;
44CGFloatappviewW=80;
45CGFloatappviewH=90;
46CGFloatmargin=(self.view.frame.size.width-totalloc*appviewW)/(totalloc+1);
47
48intcount=self.app.count;
49for(inti=0;i50
51introw=i/totalloc;
52intloc=i%totalloc;
53CGFloatappviewX=margin+(margin+appviewW)*loc;
54CGFloatappviewY=margin+(margin+appviewH)*row;
55YYapp*app=self.app[i];
56
57//拿出xib视图
58NSArray*apparray=[[NSBundlemainBundle]loadNibNamed:
@"appxib"owner:
niloptions:
nil];
59
60//注意这里的类型名!
61//UIView*appview=[apparrayfirstObject];
62YYappview*appview=[apparrayfirstObject];
63
64//加载视图
65appview.frame=CGRectMake(appviewX,appviewY,appviewW,appviewH);
66[self.viewaddSubview:
appview];
67
68appview.appimg.image=app.image;
69appview.applab.text=app.name;
70appview.appbtn.tag=i;
71
72[appview.appbtnaddTarget:
selfaction:
@selector(appviewbtnClick:
)forControlEvents:
UIControlEventTouchUpInside];
73
74}
75}
76
77/**按钮的点击事件*/
78-(void)appviewbtnClick:
(UIButton*)btn
79{
80YYapp*apps=self.app[btn.tag];
81UILabel*showlab=[[UILabelalloc]initWithFrame:
CGRectMake(60,450,200,20)];
82[showlabsetText:
[NSStringstringWithFormat:
@"%@下载成功",apps.name]];
83[showlabsetBackgroundColor:
[UIColorlightGrayColor]];
84[self.viewaddSubview:
showlab];
85showlab.alpha=1.0;
86
87//简单的动画效果
88[UIViewanimateWithDuration:
2.0animations:
^{
89showlab.alpha=0;
90}completion:
^(BOOLfinished){
91[showlabremoveFromSuperview];
92}];
93}
94
95@end
YYappview.h文件代码(已经连线)
#import
@interfaceYYappview:
UIView
@property(strong,nonatomic)IBOutletUIImageView*appimg;
@property(strong,nonatomic)IBOutletUILabel*applab;
@property(strong,nonatomic)IBOutletUIButton*appbtn;
@end