美文网首页
MVVM在iOS中的使用

MVVM在iOS中的使用

作者: 麦子maizi | 来源:发表于2021-12-22 11:30 被阅读0次

1.理解:

MVVM是一种设计模式,本质在于ViewModel与View或Controller层进行绑定,model层数据变化可以通过ViewModel直接更新UI。

开发过程中,从可读性和开发效率上简单的视图倾向于Controller持有特定的ViewModel。

2.优势

(1)简化开发

一个Controller针对某一任务持有其ViewModel,而非View或者subView持有,只需要维护Controller这一层的ViewModel;

(2)解耦

让View专注于视图和视图逻辑,ViewModel专注于逻辑处理、网络请求、数据解析以及暴露数据接口;

(3)便于测试,高复用性

View和ViewModel由于低耦合性,可以方便被其它模块调用。ViewModel可以单独调试。

3.使用场景

场景:博文列表

属性 类型
authorLabel作者 UILabel
avatarImgView头像 UIImageView
contentLabel博文 UILabel

Cell层,提供外部访问的组件

@interface BlogCell : TYBaseTableViewCell

/// 作者
@property (nonatomic, strong) UILabel *authorLabel;

/// 头像
@property (nonatomic, strong) UIImageView *avatarImgView;

/// 博文
@property (nonatomic, strong) UILabel *contentLabel;

@end

ViewModel层,提供数据源,提供方式给C层Subscribe,从而刷新数据,以下提供3种方式状态值,RACSubject,RACCommand

@interface BlogViewModel : NSObject

/// 数据源
@property (nonatomic, strong) NSMutableArray *dataArray;

/// 状态:刷新状态
@property (nonatomic, assign) TYStatus status;

/// 单向
@property (nonatomic, strong) RACSubject *subject;

/// 双向
@property (nonatomic, strong) RACCommand *command;

@end

Controller层,视图的配置和初始化等,主要有3块:1、cell的赋值 2、UI逻辑下拉刷新 3、UI逻辑加载更多

    // Cell的配置和赋值
    [listView setConfigCellBlock:^UITableViewCell * _Nonnull(UITableView * _Nonnull table, NSIndexPath * _Nonnull indexPath) {
        BlogCell *cell = [table dequeueReusableCellWithIdentifier:NSStringFromClass([BlogCell class])];
        if (indexPath.row < weakSelf.blogVM.dataArray.count) {
            id info = weakSelf.blogVM.dataArray[indexPath.row];
            cell.avatarImgView.image = [UIImage imageNamed:info[@"avatar"]];
            cell.authorLabel.text    = info[@"author"];
            cell.contentLabel.text   = info[@"content"];
        }
        return cell;
    }];
    // 刷新
    [listView setRefreshBlock:^{
        [weakSelf request];
    }];
    // 加载更多
    [listView setLoadMoreBlock:^{
        [weakSelf loadMore];
    }];

Controller层,绑定ViewModel和更新View,以下提供2种方式,RACSubject和自定义状态

- (void)bind {
    @weakify(self);
    [self.blogVM.subject subscribeNext:^(id  _Nullable x) {
            // ....
    }];

    [RACObserve(self.blogVM, status)subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        NSInteger val = [x integerValue];
        if (val == TYStatusRefreshCompleted) {
            [self.listView endRefresh];
            [self.listView reloadData];
        }
        else if (val == TYStatusLoadCompleted) {
            [self.listView endLoadingMore];
            [self.listView reloadData];
        }
    }];
}

相关文章

  • MVVM在iOS中的使用

    1.理解: MVVM是一种设计模式,本质在于ViewModel与View或Controller层进行绑定,mode...

  • iOS 使用MVVM模式实现Cell的点击响应

    iOS 使用MVVM模式实现Cell的点击响应 iOS 使用MVVM模式实现Cell的点击响应

  • iOS MVVM在UITableView中的使用

    前言 目前公司升级系统,需要使用MVVM来展示复杂的页面,如详情等这里说明 越是复杂的详情 越需要使用UITabl...

  • iOS MVVM架构总结

    参考:iOS 中MVC设计模式iOS MVVM架构iOS MVVM-框架介绍iOS 架构模式MVVM的实践总结iO...

  • MVVM

    问题:1、MVVM中的网络层在MVVM三个模块中的哪一块? 参考链接:MVVM最佳解决实践通过MVVM进行iOS开...

  • iOS MVC、MVP、MVVM的正确使用姿势

    iOS使用RAC实现MVVM的正经姿势 从MVC到MVVM [http://blog.harrisonxi.com...

  • iOS MVVM架构,简单理解

    iOS MVVM架构 iOS中,我们使用的大部分都是MVC架构虽然MVC的层次明确,但是由于功能日益的增加,代码的...

  • IOS RAC实践

    前言 RAC使用-->IOS RAC使用 -- ReactiveObjC 本文使用RAC+MVVM来模拟用户登录 ...

  • 字典转模型的runtime实现

    前言 我们在iOS开发中,一般会使用MVC或者MVVM等模式。当我们从接口中拿到数据时,我们需要把数据转成模型使用...

  • ios-教你一步一步实现自己的字典转模型库

    前言 我们在iOS开发中,一般会使用MVC或者MVVM等模式。当我们从接口中拿到数据时,我们需要把数据转成模型使用...

网友评论

      本文标题:MVVM在iOS中的使用

      本文链接:https://www.haomeiwen.com/subject/uklqqrtx.html