zoukankan      html  css  js  c++  java
  • MVVM的项目学习和笔记

    今天在学习一个用MVVM模式写的项目,掌握一下对MVVM的理解和记的一些笔记.

    下面是自己学习的项目链接:

    http://www.code4app.com/ios/一个MVVM架构的iOS工程/695f5862-f1d5-11e5-af82-00163e0606f4

    1.

    一个MVVM架构的iOS工程 :

    Model层是少不了的了,我们得有东西充当DTO(数据传输对象),当然,用字典也是可以的,Model层是比较薄的一层; 

    ViewModel层,就是View和Model层的粘合剂,他是一个放置用户输入验证逻辑,视图显示逻辑,发起网络请求和其他各种各样的代码的极好的地方。说白了,就是把原来ViewController层的业务逻辑和页面逻辑等剥离出来放到ViewModel层。 

    View层,就是ViewController层,他的任务就是从ViewModel层获取数据,然后显示。

     

    2.

    1>

    自定义Model类:

    @interface PublicModel : NSObject

    @property (strong, nonatomic) NSString *userId;

    @property (strong, nonatomic) NSString *weiboId;

    @property (strong, nonatomic) NSString *userName;

    @property (strong, nonatomic) NSURL *imageUrl;

    @property (strong, nonatomic) NSString *date;

    @property (strong, nonatomic) NSString *text;

     @end

     

     

    ViewModel部分:

    1>自定义Model1类,接收数据:

    1 #pragma 接收穿过来的block
    2 -(void) setBlockWithReturnBlock: (ReturnValueBlock) returnBlock
    3                  WithErrorBlock: (ErrorCodeBlock) errorBlock
    4                WithFailureBlock: (FailureBlock) failureBlock
    5 {
    6     _returnBlock = returnBlock;
    7     _errorBlock = errorBlock;
    8     _failureBlock = failureBlock;
    9 }

    2>再定义Model2类,继承自Model1类:

    进行网络请求,对请求下来的数据进行数据处理,处理跳转到详情页面.

     1 @implementation PublicWeiboViewModel
     2 
     3 //获取公共微博
     4 -(void) fetchPublicWeiBo
     5 {
     6     NSDictionary *parameter = @{TOKEN: ACCESSTOKEN,
     7                                 COUNT: @"100"
     8                                 };
     9     [NetRequestClass NetRequestGETWithRequestURL:REQUESTPUBLICURL WithParameter:parameter WithReturnValeuBlock:^(id returnValue) {
    10         
    11         DDLog(@"%@", returnValue);
    12         [self fetchValueSuccessWithDic:returnValue];
    13         
    14     } WithErrorCodeBlock:^(id errorCode) {
    15         DDLog(@"%@", errorCode);
    16         [self errorCodeWithDic:errorCode];
    17         
    18     } WithFailureBlock:^{
    19         [self netFailure];
    20         DDLog(@"网络异常");
    21         
    22     }];
    23     
    24 }
    25 
    26 
    27 
    28 #pragma 获取到正确的数据,对正确的数据进行处理
    29 -(void)fetchValueSuccessWithDic: (NSDictionary *) returnValue
    30 {
    31     //对从后台获取的数据进行处理,然后传给ViewController层进行显示
    32     
    33     NSArray *statuses = returnValue[STATUSES];
    34     NSMutableArray *publicModelArray = [[NSMutableArray alloc] initWithCapacity:statuses.count];
    35     
    36     for (int i = 0; i < statuses.count; i ++) {
    37         PublicModel *publicModel = [[PublicModel alloc] init];
    38         
    39         //设置时间
    40         NSDateFormatter *iosDateFormater=[[NSDateFormatter alloc]init];
    41         iosDateFormater.dateFormat=@"EEE MMM d HH:mm:ss Z yyyy";
    42         
    43         //必须设置,否则无法解析
    44         iosDateFormater.locale=[[NSLocale alloc]initWithLocaleIdentifier:@"en_US"];
    45         NSDate *date=[iosDateFormater dateFromString:statuses[i][CREATETIME]];
    46         
    47         //目的格式
    48         NSDateFormatter *resultFormatter=[[NSDateFormatter alloc]init];
    49         [resultFormatter setDateFormat:@"MM月dd日 HH:mm"];
    50         
    51         publicModel.date = [resultFormatter stringFromDate:date];
    52         publicModel.userName = statuses[i][USER][USERNAME];
    53         publicModel.text = statuses[i][WEIBOTEXT];
    54         publicModel.imageUrl = [NSURL URLWithString:statuses[i][USER][HEADIMAGEURL]];
    55         publicModel.userId = statuses[i][USER][UID];
    56         publicModel.weiboId = statuses[i][WEIBOID];
    57         
    58         [publicModelArray addObject:publicModel];
    59         
    60     }
    61     
    62     self.returnBlock(publicModelArray);
    63 }
    64 
    65 #pragma 对ErrorCode进行处理
    66 -(void) errorCodeWithDic: (NSDictionary *) errorDic
    67 {
    68     self.errorBlock(errorDic);
    69 }
    70 
    71 #pragma 对网路异常进行处理
    72 -(void) netFailure
    73 {
    74     self.failureBlock();
    75 }
    76 
    77 
    78 #pragma 跳转到详情页面,如需网路请求的,可在此方法中添加相应的网络请求
    79 -(void) weiboDetailWithPublicModel: (PublicModel *) publicModel WithViewController:(UIViewController *)superController
    80 {
    81     DDLog(@"%@,%@,%@",publicModel.userId,publicModel.weiboId,publicModel.text);
    82     UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
    83     PublicDetailViewController *detailController = [storyboard instantiateViewControllerWithIdentifier:@"PublicDetailViewController"];
    84     detailController.publicModel = publicModel;
    85     [superController.navigationController pushViewController:detailController animated:YES];
    86     
    87 }
    88 
    89 
    90 @end

    3.Controller部分:

    接收model数据,定义表视图,实现其协议方法.

     1 @implementation PublicTableViewController
     2 
     3 - (void)viewDidLoad {
     4     [super viewDidLoad];
     5     
     6     
     7     // 接收数据
     8     
     9     PublicWeiboViewModel *publicViewModel = [[PublicWeiboViewModel alloc] init];
    10     
    11     [publicViewModel setBlockWithReturnBlock:^(id returnValue) {
    12         
    13         [SVProgressHUD dismiss];
    14         _publicModelArray = returnValue;
    15         [self.tableView reloadData];
    16         DDLog(@"%@",_publicModelArray);
    17         
    18     } WithErrorBlock:^(id errorCode) {
    19         
    20         [SVProgressHUD dismiss];
    21         
    22     } WithFailureBlock:^{
    23         
    24         [SVProgressHUD dismiss];
    25         
    26     }];
    27     
    28     [publicViewModel fetchPublicWeiBo];
    29     
    30     [SVProgressHUD showWithStatus:@"正在获取用户信息……" maskType:SVProgressHUDMaskTypeBlack];
    31     
    32 }
    33 
    34 - (void)didReceiveMemoryWarning {
    35     [super didReceiveMemoryWarning];
    36     // Dispose of any resources that can be recreated.
    37 }
    38 
    39 #pragma mark - Table view data source
    40 
    41 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    42     return 1;
    43 }
    44 
    45 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    46     
    47     return _publicModelArray.count;
    48 }
    49 
    50 
    51 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    52     PublicCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PublicCell" forIndexPath:indexPath];
    53     
    54     [cell setValueWithDic:_publicModelArray[indexPath.row]];
    55     
    56     return cell;
    57 }
    58 
    59 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    60 {
    61     PublicWeiboViewModel *publicViewModel = [[PublicWeiboViewModel alloc] init];
    62     [publicViewModel weiboDetailWithPublicModel:_publicModelArray[indexPath.row] WithViewController:self];
    63 }

    总结:

    1.其实际上MVVM和MVC设计模式大体上是一样的,只是把MVC在C中请求网络和处理数据信息的部分子类了在ViewModel中实现,减轻了C的负担和代码量;

    2.Model的存储数据还是不变;

  • 相关阅读:
    EditPlus 4.3.2502 中文版已经发布(12月5日更新)
    EditPlus 4.3.2499 中文版已经发布(11月21日更新)
    为什么有负频率,什么是相位谱 —— 关于傅立叶变换的随笔
    对“善于提问,主动解决问题”的程序员的吐槽
    .NET事件监听机制的局限与扩展
    .NET陷阱之六:从枚举值持久化带来大量空间消耗谈起
    深入挖掘.NET序列化机制——实现更易用的序列化方案
    在VMWare中建立Hadoop虚拟集群的详细步骤(使用CentOS)
    微信分享
    如何导入ShareSDK的sample
  • 原文地址:https://www.cnblogs.com/pengsi/p/5377148.html
Copyright © 2011-2022 走看看