ios中MVP,MVC,MVVM 的使用方法

1.我们的很多框架都使用mvc这种经典的框架去搭建项目了,这里主要讲下mvvm的框架了:

2.mvvm框架是新出来的一种设计框架了,我们这里可以理解下: 

 controller -----> view--------->model       viewModel (这个里面就是controller的逻辑出来)---->view() ------>model(这里是执行数据的操作处理呀)

3.看代码的:

model:

@property(nonatomic,copy)NSString*intro;

@property(nonatomic,copy)NSString*kpic;

@property(nonatomic,copy)NSString*longTitle;


cell代码:

- (void)setModel:(IconModle*)model{

_model= model;

_titleLable.text=_model.intro;

_longTitle.text=_model.longTitle;

NSURL*url=[NSURLURLWithString:_model.kpic];

[_IconImageViewsd_setImageWithURL:url];

}

viewmodel:

viewmodel.h:

typedefvoid(^returnValueBlock) (idreturnValue);

typedefvoid(^returenErrorCodeBlock) (iderrorCode);

@interfaceIconViewModle :NSObject

@property(nonatomic,copy)returnValueBlockreturnValue;

@property(nonatomic,copy)returenErrorCodeBlockerrorCode;

-(void)getIconData;


viewmodel.m

-(void)getIconData{

AFHTTPSessionManager*manager = [AFHTTPSessionManagermanager];

[managerGET:@"http://newsapi.sina.cn/?resource=feed&accessToken=&chwm=3023_0001&city=CHXX0008&connectionType=2&deviceId=3d91d5d90c90486cde48597325cf846b699ceb53&deviceModel=apple-iphone5&from=6053093012&idfa=7CE5628E-577A-4A0E-B9E5-283217ECA1F1&idfv=10E31C9D-59AE-4547-BDEF-5FF3EA045D86&imei=3d91d5d90c90486cde48597325cf846b699ceb53&location=39.998602%2C116.365189&osVersion=9.3.5&resolution=640x1136&token=61903050f1141245bfb85231b58e84fb586743436ceb50af9f7dfe17714ee6f7&ua=apple-iphone5__SinaNews__5.3__iphone__9.3.5&weiboSuid=&weiboUid=&wm=b207&rand=221&urlSign=3c861405dd&behavior=manual&channel=news_pic&lastTimestamp=1473578882&listCount=20&p=1&pullDirection=down&pullTimes=8&replacedFlag=1&s=20"parameters:nilprogress:^(NSProgress*_NonnulldownloadProgress) {

}success:^(NSURLSessionDataTask*_Nonnulltask,id_NullableresponseObject) {

NSLog(@"........get请求url:%@",responseObject);

NSDictionary*dic = responseObject[@"data"];

NSArray*subjects = dic[@"feed"];

NSMutableArray*modelArr = [NSMutableArrayarrayWithCapacity:subjects.count];

for(NSDictionary*subjectindic[@"feed"])

{

IconModle*icon=[[IconModlealloc]init];

icon.intro= subject[@"intro"];

icon.kpic= subject[@"kpic"];

icon.longTitle= subject[@"longTitle"];

[modelArraddObject:icon];

}

_returnValue(modelArr);

}failure:^(NSURLSessionDataTask*_Nullabletask,NSError*_Nonnullerror) {

NSLog(@"error = %@", error);

}];

}


controller的代码:

- (void)viewDidLoad {

self.title=@"MVVM例子二";

[superviewDidLoad];

[selfsetupUI];

}

-(void)setupUI{

UITableView*table=[[UITableViewalloc]initWithFrame:self.view.bounds];

table.delegate=self;

table.dataSource=self;

table.rowHeight=300;

table.tableFooterView=[UIViewnew];

[self.viewaddSubview:table];

IconViewModle*ViewModle=[[IconViewModlealloc]init];

ViewModle.returnValue=^(idreturnValeu){

_modelArray=returnValeu;

[tablereloadData];

};

ViewModle.errorCode=^(iderrorCode){

};

[ViewModlegetIconData];

}

-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section

{

return_modelArray.count;

}

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath

{

staticNSString* identifer =@"IconCell";

IconCell* cell = [tableViewdequeueReusableCellWithIdentifier:identifer];

if(!cell)

{

NSArray*array = [[NSBundlemainBundle]loadNibNamed:@"IconCell"owner:niloptions:nil];

cell=[arrayobjectAtIndex:0];

}

cell.model=_modelArray[indexPath.row];

returncell;

}


总结下:viewmodel :里面写的controller的逻辑处理的方法了 view 里面写的是数据的获取的方法 

你可能感兴趣的:(ios中MVP,MVC,MVVM 的使用方法)