iOS--MVC设计模式

MVC简单的说就是Model View Controller(模型,视图,控制器),其用意就是将数据与视图分离开来,充分理解iOS的MVC模式,有助于我们程序的组织和理性。

MVC的明显特征和体现:

1.View视图的显示,取决于Model,只要Model数据改变了,View的显示状态也会跟着改变

2.Controller负责初始化Model,并将Model传递给View去解析展示

3.简单的MVC即控制器加载模型数据并数据转化为数据模型,控制器创建视图控件,并将模型数据传递给视图控件。

4.MVC的缺点:较差的可测试性,由于控制器混合了视图处理逻辑和业务逻辑,分离这些成分的单元测试成了一个艰巨的任务,大多数人选择忽略这个任务,那就是不做任何测试。

5.MVC的优点:低耦合性、高重用性和可适用性、可维护性(分离视图层和业务逻辑层也使得应用更易于维护和修改)。

M------模型


#import@interface Worker : NSObject

@interface Worker : NSObject

@property (nonatomic,strong) NSString* name;//姓名

@property (nonatomic,assign) NSString* age;//年龄

@property (nonatomic,strong) NSString* workType;//职业

@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];

//职业

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];

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容