ios 应用程序管理的实现
1. 项目名称:应用管理
2. 项目截图展示
3. 项目功能
展示应用图标,名称和下载按钮
点击下载按钮,出现“正在下载”图标
4. 项目代码
模型代码:appinfo.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
#import <foundation/foundation.h>
#import <uikit/uikit.h>
@interface appinfo : nsobject
@property (nonatomic, copy) nsstring *name;
@property (nonatomic, copy) nsstring *icon;
@property (nonatomic, strong, readonly) uiimage *image;
/** 使用字典实例化模型 */
- (instancetype)initwithdict:(nsdictionary *)dict;
/** 快速实例化一个对象 */
+ (instancetype)appinfowithdict:(nsdictionary *)dict;
/** 返回所有plist中的数据模型数组 */
+ (nsarray *)applist;
@end
|
模型代码:appinfo.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
#import "appinfo.h"
@implementation appinfo
// 合成指令,主动指定属性使用的成员变量名称
@synthesize image = _image;
//图片模型
- (uiimage *)image
{
if (_image == nil) {
_image = [uiimage imagenamed:self.icon];
}
return _image;
}
- (instancetype)initwithdict:(nsdictionary *)dict
{
self = [super init];
if (self) {
// 用字典给属性赋值
// self.name = dict[@"name"]; //将字典的内容赋值给属性
// self.icon = dict[@"icon"];
[self setvaluesforkeyswithdictionary:dict];
}
return self;
}
+ (instancetype)appinfowithdict:(nsdictionary *)dict
{
return [[self alloc] initwithdict:dict];
}
+ (nsarray *)applist
{
nsarray *array = [nsarray arraywithcontentsoffile:[[nsbundle mainbundle] pathforresource:@ "app.plist" oftype:nil]];
// 创建一个临时可变数组
nsmutablearray *arraym = [nsmutablearray array];
// 遍历数组,依次转换模型
for (nsdictionary *dict in array) {
[arraym addobject:[appinfo appinfowithdict:dict]];
}
return arraym;
}
@end
|
模型view:appview.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
#import <uikit/uikit.h>
@ class appinfo;
@interface appview : uiview
/** 类方法,方便调用视图 */
+ (instancetype)appview;
/** 实例化视图,并使用appinfo设置视图的显示 */
+ (instancetype)appviewwithappinfo:(appinfo *)appinfo;
// 自定义视图中显示的数据来源是数据模型
// 使用模型设置自定义视图的显示
@property (nonatomic, strong) appinfo *appinfo;
@end
|
模型view:appview.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
#import "appview.h"
#import "appinfo.h"
@interface appview()
@property (weak, nonatomic) iboutlet uiimageview *iconview;
@property (weak, nonatomic) iboutlet uilabel *label;
@end
@implementation appview
//实例化xib
+ (instancetype)appview
{
return [[[nsbundle mainbundle] loadnibnamed:@ "appview" owner:nil options:nil] lastobject];
}
//根据模型实例化xib
+ (instancetype)appviewwithappinfo:(appinfo *)appinfo
{
// 1. 实例化一个视图
appview *view = [self appview];
// 2. 设置视图的显示
view.appinfo = appinfo; //包含,appview有appinfo的属性
// 3. 返回视图
return view;
}
/**
利用setter方法设置视图的界面显示
*/
- ( void )setappinfo:(appinfo *)appinfo
{
_appinfo = appinfo;
self.label.text = appinfo.name;
self.iconview.image = appinfo.image;
}
/** 按钮监听方法 */
- (ibaction)click:(uibutton *)button
{
// 添加一个uilabel到界面上
uilabel *label = [[uilabel alloc] initwithframe:cgrectmake(80, 400, 160, 40)];
// 数值是0表示黑色,1表示纯白;alpha表示透明度
label.backgroundcolor = [uicolor colorwithwhite:0.0 alpha:0.2];
label.text = self.appinfo.name;
label.textalignment = nstextalignmentcenter;
// self.superview就是视图控制器中的self.view
[self.superview addsubview:label];
// 动画效果
label.alpha = 0.0;
// 禁用按钮,如果点击了按钮以后就禁用按钮
button.enabled = no;
// 动画结束之后删除
[uiview animatewithduration:1.0f animations:^{
// 要修改的动画属性
label.alpha = 1.0;
} completion:^( bool finished) {
[uiview animatewithduration:1.0 animations:^{
label.alpha = 0.0;
} completion:^( bool finished)
[label removefromsuperview];
}];
}];
}
@end
|
viewcontroller.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
#import "viewcontroller.h"
#import "appinfo.h"
#import "appview.h"
#define kappvieww 80
#define kappviewh 90
#define kcolcount 3
#define kstarty 20
@interface viewcontroller ()
/** 应用程序列表 */
@property (nonatomic, strong) nsarray *applist;
@end
@implementation viewcontroller
- (nsarray *)applist
{
if (_applist == nil) {
_applist = [appinfo applist];
}
return _applist;
}
- ( void )viewdidload
{
[super viewdidload];
// 搭建九宫格
// 320 - 3 * 80 = 80 / 4 = 20
cgfloat marginx = (self.view.bounds.size.width - kcolcount * kappvieww) / (kcolcount + 1);
cgfloat marginy = 10;
for ( int i = 0; i < self.applist.count; i++) {
// 行
int row = i / kcolcount;
// 列
int col = i % kcolcount;
cgfloat x = marginx + col * (marginx + kappvieww);
cgfloat y = kstarty + marginy + row * (marginy + kappviewh);
//加载第i个xib视图
appview *appview = [appview appviewwithappinfo:self.applist[i]];
// 设置视图位置
appview.frame = cgrectmake(x, y, kappvieww, kappviewh);
[self.view addsubview:appview];
}
}
|
5. 本项目必须掌握的代码段
字典转模型
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
- (instancetype)initwithdict:(nsdictionary *)dict
{
self = [super init];
if (self) {
[self setvaluesforkeyswithdictionary:dict];
}
return self;
}
+ (instancetype)appinfowithdict:(nsdictionary *)dict
{
return [[self alloc] initwithdict:dict];
}
+ (nsarray *)applist
{
nsarray *array = [nsarray arraywithcontentsoffile:[[nsbundle mainbundle] pathforresource:@ "app.plist" oftype:nil]];
// 创建一个临时可变数组
nsmutablearray *arraym = [nsmutablearray array];
// 遍历数组,依次转换模型
for (nsdictionary *dict in array) {
[arraym addobject:[appinfo appinfowithdict:dict]];
}
return arraym;
}
|
kvc
1
|
[self setvaluesforkeyswithdictionary:dict];
|
6. 笔记
字典转模型:
plist文件有多个字典,把字典的元素转换成模型类对象的成员变量,将模型类对象放入数组中 模型的属性名称和plist文件中的key名称必须一致
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
原文链接:http://blog.csdn.net/apple890111/article/details/48861641