美文网首页iOS归纳
iOS 开发之 MVP

iOS 开发之 MVP

作者: 红色海_ | 来源:发表于2020-03-19 22:02 被阅读0次

有借鉴 有修改 为了自己 定期深化理解。。

MVP模式是怎样的呢? 看下图

MVP.png

MVP模式示意图

从上图可以看出, 从MVC中又抽象出了P层, 即Presenter层

  • Controller其实将view和viewController传递给了P层, 这样P层其实就拥有了控制器的权利, 完全可以行使控制器的职责.

  • Controller又持有Presenter, 那么它只需要调用P层暴露出的接口, 就完全可以完成整个业务逻辑和页面展示

关于C端和P端的循环引用的问题, 直接用weak关键字就可以解决了

利用代码来说明一下问题:

  • 这是一个Presenter的Protocol, 所有的P层的类都要遵循这个Protocol
#import /**
  作为P : presenter 是管理 view viewController model这个三个中间人,负责UI刷新
  视图的交互总是和VC 关联着的
 */
@protocol TGPresenterProtocol @optional
// 处理View视图相关操作 -- 协议的遵守者
- (void)setView:(NSObject *)view;
// 处理事件的相关响应
- (void)setViewController:(UIViewController *)viewController;
// 展示
- (void)present;
// 加载model 
- (void)presentWithModel:(id)model viewController:(UIViewController *)viewController;
@end
  • 可以看出, P层是可以拿到view或者viewController的, 并且可以在实现set方法的时候做一些事情. 这个稍后再讲

  • 另外, P层还可以展示数据, 直接展示数据, present方法, 利用模型展示数据, 利用presentWithModel:方法

  • 比如, 在一个遵循了TGPresenterProtocol的Presenter类中

把需要管理的view传递给P,

- (instancetype)initWithTableView:(UITableView *)view{

    self = [super init];
    if (!self) {
        return nil;
    }
    _view = view;
    _view.delegate = self;
    _view.dataSource = self;
    _view.separatorStyle = UITableViewCellSeparatorStyleNone;
    // 自适应高度
    _view.rowHeight = UITableViewAutomaticDimension;
    _view.estimatedRowHeight = 100;
    return self;
}
- (void)setView:(UITableView *)view{
    // 设置视图
    _view = view;
    _view.delegate = self;
    _view.dataSource = self;
    _view.separatorStyle = UITableViewCellSeparatorStyleNone;
    // 自适应高度
    _view.rowHeight = UITableViewAutomaticDimension;
    _view.estimatedRowHeight = 100;
}
  • 比如上面的代码, 将tableView的数据源和代理都给了P, 那么P就相当于行使了控制器的权力, 当P层拿到数据时(没错, P层是持有Model的):
- (void)loadHPData{

    NSString *dataPath = [[NSBundle mainBundle] pathForResource:@"testCellData" ofType:@"json"];
    NSData *jsonData = [NSData dataWithContentsOfFile:dataPath];
    NSError *error;
    NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:&error];
    if (error) {
        NSLog(@"error = %@",error.localizedDescription);
    }
    NSLog(@"dataDic = %@",dataDic);
    // model 要处理好数据的显示格式
    self.hpModel = [[CellSelfSizeModel alloc] initWithDic:dataDic];
    // 刷新
    [self present];

}
  • 走Present方法, 实际就是tableView的reloadData:
- (void)present{

    [self.view reloadData];
}
  • 然后重走tableView的数据源方法. 将数据分发给cell去展示:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.hpModel.data.listArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    NSString *ID = @"";
    cellType type;

    CelllSelfSizeListModel *newsList;
    if (indexPath.row > self.hpModel.data.listArray.count - 1) {
        newsList = nil;
    }else{
        newsList = self.hpModel.data.listArray[indexPath.row];
    }
    if (newsList.orginImg.length>0) {
        // 有图片
        type = NewsInListCellTypeHavePic;

    }else{
        // 无图片
        type = NewsInListCellTypeOnlyWord;
    }

    ID = [NSString stringWithFormat:@"reusId%ld",(long)type];

    SelfSizeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[SelfSizeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID cellType:type];
    }

    cell.cellModel = newsList;

    return cell;
}

这样就实现了Controller, View, Model的解耦. 给大家看看控制器做的事情:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.title = @"MVP Demo";
//    布局
    [self initViews];
    [self setUpConstraints];

    self.hpPresenter = [TGHPPresenter new];
    // 视图对象
    self.hpPresenter.view = self.tableView;
    // 控制器对象
    self.hpPresenter.viewController = self;
    // 外边是要传入参进去的 -- 数据模型
    [self.hpPresenter loadHPData];

}

只需要初始化P层, 然后调P层的接口就可以了. 至于P层内部的逻辑, 我不需要知道

  • V层也只专注于视图的创建

  • M层只专注于模型的构建(字典->模型)

相关文章

网友评论

    本文标题:iOS 开发之 MVP

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