MVC簡單的說就是Model View Controller(模型,視圖评矩,控制器)抠刺,其用意就是將數(shù)據(jù)與視圖分離開來塔淤,充分理解iOS的MVC模式,有助于我們程序的組織和理性速妖。
MVC的明顯特征和體現(xiàn):
1.View視圖的顯示高蜂,取決于Model,只要Model數(shù)據(jù)改變了罕容,View的顯示狀態(tài)也會跟著改變
2.Controller負責初始化Model备恤,并將Model傳遞給View去解析展示
3.簡單的MVC即控制器加載模型數(shù)據(jù)并數(shù)據(jù)轉(zhuǎn)化為數(shù)據(jù)模型稿饰,控制器創(chuàng)建視圖控件,并將模型數(shù)據(jù)傳遞給視圖控件露泊。
4.MVC的缺點:較差的可測試性喉镰,由于控制器混合了視圖處理邏輯和業(yè)務邏輯,分離這些成分的單元測試成了一個艱巨的任務惭笑,大多數(shù)人選擇忽略這個任務侣姆,那就是不做任何測試。
5.MVC的優(yōu)點:低耦合性沉噩、高重用性和可適用性捺宗、可維護性(分離視圖層和業(yè)務邏輯層也使得應用更易于維護和修改)。
M------模型
#import@interface Worker : NSObject
@interface Worker : NSObject
@property (nonatomic,strong) NSString* name;//姓名
@property (nonatomic,assign) NSString* age;//年齡
@property (nonatomic,strong) NSString* workType;//職業(yè)
@property (nonatomic,assign) NSString* wage;//工資
@property (nonatomic,assign) NSString* workTime;//工作年限
@property (nonatomic,strong) NSDictionary* dict;
- (instancetype)initWithDict:(NSDictionary *)dic;
@end
#import "Worker.h"
@implementation Worker
- (instancetype)initWithDict:(NSDictionary *)dic{
if (self = [super init]) {
self.dict = dic;
}
return self;
}
- (void)setDict:(NSDictionary *)dict{
self.name = [dict objectForKey:@"name"];
self.age = [dict objectForKey:@"age"] ;
self.workType = [dict objectForKey:@"workType"];
self.wage = [dict objectForKey:@"wage"] ;
self.workTime = [dict objectForKey:@"workTime"];
}
@end
V------視圖-view
#import#import "Worker.h"
@interface WorkerInfoView : UIView
@property (nonatomic,strong) UILabel* nameLab;
@property (nonatomic,strong) UILabel* workTypeStr;
@property (nonatomic,strong) UILabel* workTimeLab;
@property (nonatomic,strong) UILabel* wageLab;
@property (nonatomic,strong) UILabel* ageLab;
@property (nonatomic,strong) Worker* worker;
@end
#import "WorkerInfoView.h"
#import "UIViewExt.h"
@implementation WorkerInfoView
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
//名字
self.nameLab = [[UILabel alloc]init];
[self addSubview:self.nameLab];
self.nameLab.textAlignment = NSTextAlignmentCenter;
self.nameLab.font = [UIFont systemFontOfSize:14.0f];
self.nameLab.textColor = [UIColor blackColor];
//職業(yè)
self.workTypeStr = [[UILabel alloc]init];
[self addSubview:self.workTypeStr];
self.workTypeStr.textAlignment = NSTextAlignmentCenter;
self.workTypeStr.font = [UIFont systemFontOfSize:14.0f];
self.workTypeStr.textColor = [UIColor blackColor];
//工作時間
self.workTimeLab = [[UILabel alloc]init];
[self addSubview:self.workTimeLab];
self.workTimeLab.textAlignment = NSTextAlignmentCenter;
self.workTimeLab.font = [UIFont systemFontOfSize:14.0f];
self.workTimeLab.textColor = [UIColor blackColor];
//工資
self.wageLab = [[UILabel alloc]init];
[self addSubview:self.wageLab];
self.wageLab.textAlignment = NSTextAlignmentCenter;
self.wageLab.font = [UIFont systemFontOfSize:14.0f];
self.wageLab.textColor = [UIColor blackColor];
//年齡
self.ageLab = [[UILabel alloc]init];
[self addSubview:self.ageLab];
self.ageLab.textAlignment = NSTextAlignmentCenter;
self.ageLab.font = [UIFont systemFontOfSize:14.0f];
self.ageLab.textColor = [UIColor blackColor];
}
return self;
}
- (void)layoutSubviews{
self.nameLab.frame = CGRectMake(self.left+10,10, 80, 30);
self.workTypeStr.frame = CGRectMake(self.nameLab.left, self.nameLab.bottom, 80, 30);
self.workTimeLab.frame = CGRectMake(self.workTypeStr.left, self.workTypeStr.bottom, 80, 30);
self.wageLab.frame = CGRectMake(self.workTimeLab.left, self.workTimeLab.bottom, 80, 30);
self.ageLab.frame = CGRectMake(self.wageLab.left, self.wageLab.bottom, 80, 30);
}
- (void)setWorker:(Worker *)worker{
self.nameLab.text = worker.name;
self.workTypeStr.text = worker.workType;
self.workTimeLab.text = worker.workTime;
self.wageLab.text = worker.wage;
self.ageLab.text = worker.age;
}
@end
C----控制器---
NSDictionary* dict = @{@"name":@"zhaobin",@"age":@"12",@"workType":@"iOS",@"wage":@"保密",@"workTime":@"2"};
//? ? Worker* worke = [[Worker alloc]initWithDict:dict];
WorkerInfoView* infoView = [[WorkerInfoView alloc]init];
infoView.frame = CGRectMake(0, 80, 200, 200);
infoView.backgroundColor = [UIColor redColor];
Worker* worke = [[Worker alloc]init];
worke.dict = dict;
infoView.worker =worke;
[self.view addSubview:infoView];