1、创建项目
2、拖入使用到的图片和plist文件
#import@interface CZAppInfo : NSObject
@property (nonatomic,copy) NSString *name;
@property (nonatomic,copy) NSString *icon;
- (instancetype)initWithDic:(NSDictionary *)dic;
+ (instancetype)appInfoWithDic:(NSDictionary *)dic;
+ (NSArray *)appInfosList;
@end
.m
#import "CZAppInfo.h"
@implementation CZAppInfo
- (instancetype)initWithDic:(NSDictionary *)dic
{
if (self = [super init]) {
self.name = dic[@"name"];
self.icon = dic[@"icon"];
}
return self;
}
+ (instancetype)appInfoWithDic:(NSDictionary *)dic
{
return [[self alloc] initWithDic:dic];
}
+ (NSArray *)appInfosList
{
//1.1 bundle
NSBundle *bundle = [NSBundle mainBundle];
//1.2 获取plist的路径
NSString *path = [bundle pathForResource:@"app" ofType:@"plist"];
//1.3 加载plist
NSArray *dicArray = [NSArray arrayWithContentsOfFile:path];
NSMutableArray *tmpArray = [NSMutableArray array];
//1.4 字典转换模型
for (NSDictionary *dic in dicArray) {
//调用appinfo的类方法
CZAppInfo *appInfo = [CZAppInfo appInfoWithDic:dic];
[tmpArray addObject:appInfo];
}
return tmpArray;
}
@end
================================
ViewController 加载 Model 数据 并把数据传给View
//1 懒加载
- (NSArray *)appInfos
{
if (_appInfos == nil) {
//调用 appInfo的类方法,返回模型数组
_appInfos = [CZAppInfo appInfosList];
}
return _appInfos;
}
------------------
//取得当前遍历到得数据
CZAppInfo *appInfo = self.appInfos[i];
//将数据传到view上
subView.appInfo = appInfo;
========================================
下面是 View 调用Model 的方法步骤
在View.h文件中引用Model类并创建一个Model 类的属性
@class CZAppInfo;
@property (nonatomic, strong) CZAppInfo *appInfo;
在View.m文件中 读取Model 数据并给各个控件赋值
/**
* 重写属性的setter方法,给子控件赋值
*
* @param appInfo <#appInfo description#>
*/
- (void)setAppInfo:(CZAppInfo *)appInfo
{
_appInfo = appInfo;
self.nameView.text = appInfo.name;
self.iconView.image = [UIImage imageNamed:appInfo.icon];
}
====
tipView.text = [NSString stringWithFormat:@"正在下载:%@",self.appInfo.name];