以国旗选择为例
flag
flagview
viewcontroller(uipickerview)
解释:flag中:
{
- (instancetype)initWithDic:(NSDictionary *)dic
{ 字典转模型的方法实现,初始化
if (self = [super init]) {
[self setValuesForKeysWithDictionary:dic];
}
return self;
}
(instancetype)flagWithDic:(NSDictionary *)dic
{
return [[self alloc] initWithDic:dic];
}`-
(NSArray *)flagList
{
//加载plist<# model #>
NSString *path = [[NSBundle mainBundle] pathForResource:@"flags" ofType:@"plist"];
NSArray *dicArray = [NSArray arrayWithContentsOfFile:path];//字典转模型(循环将数据模型保存文模型数组)
NSMutableArray *tmpArray = [NSMutableArray array];
for (NSDictionary *dic in dicArray) {
Flag *flag = [Flag flagWithDic:dic];
[tmpArray addObject:flag];
}
return tmpArray;
}
flagview:
@property(nonatomic,strong)Flag *flag;
//实现flag的set方法:如果接收数据,可对view进行相关初始化操作
-(void)setFlag:(Flag *)flag{_flag = flag;
self.nameLabel.text = flag.name;
self.imageView.image = [UIImage imageNamed:flag.icon];
}
viewcontroller:
@property (nonatomic,strong) NSArray *flags;
//懒加载,得到模型数组,实现flags的get方法
-(NSArray *)flags{
if (!_flags) {
_flags = [Flag flagList];
}
return _flags;
}
//调用flagview.flag的set方法,传入数据。
Flag *flag = self.flags[row];
flagView.flag = flag;