Controller瘦身

作者: ys简单0 | 来源:发表于2017-05-03 17:51 被阅读24次

在我们使用传统的MVC设计模式的时候,通常写着写着C中的代码就会变得又多又乱,今天通过一个简单的实例来了解MVP设计模式(即Model,View,Present).
Present负责了ModelView之间的交流和沟通,相当于是他们两个之间的 桥梁,并且在Present中可以承担一些原来在Controller中的逻辑,比如数据请求等,这样在Controller中只是实现简单的依赖设置和生命周期以及代理方法即可.具体事例如下:
首先创建Model

//只是简单的设置一个`content`属性
.h
@interface MVPModel : NSObject
@property(nonatomic,strong)NSString *content;
@end
.m
@implementation MVPModel

@end

创建View

.h
//代理
@protocol MVPViewDelegate <NSObject>

-(void)btClicked:(UIButton *)bt;

@end

@interface MVPView : UIView
@property(nonatomic,strong)UILabel *label;
@property(nonatomic,strong)UIButton *bt;
@property(nonatomic,weak)id<MVPViewDelegate>delegate;
@end
.m
//重写初始化方法
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self makeUI];
    }
    return self;
}
//页面布局
-(void)makeUI{
    self.backgroundColor = [UIColor whiteColor];
    self.label = [[UILabel alloc]initWithFrame:CGRectMake(self.frame.size.width/2-80, 100, 160, 50)];
    _label.textAlignment = NSTextAlignmentCenter;
    _label.backgroundColor = [UIColor lightGrayColor];
    _label.layer.cornerRadius = 5;
    _label.layer.masksToBounds = YES;
    [self addSubview:_label];
    
    self.bt = [UIButton buttonWithType:UIButtonTypeSystem];
    _bt.frame = CGRectMake(self.frame.size.width/2-80, 200, 160, 50);
    _bt.backgroundColor = [UIColor lightGrayColor];
    _bt.layer.cornerRadius = 5;
    [_bt setTitle:@"点我" forState:UIControlStateNormal];
    [_bt addTarget:self action:@selector(btAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_bt];
}
//点击方法
-(void)btAction:(UIButton *)bt{
    [_delegate btClicked:bt];
}

创建Present

.h
@interface Presenter : NSObject <MVPViewDelegate>
@property(nonatomic,strong)MVPModel *mvpModel;
@property(nonatomic,strong)MVPView *mvpView;

-(void)presentViewWithContent;
@end
.m
-(void)presentViewWithContent{
    self.mvpModel.content = @"123";
    self.mvpView.label.text = self.mvpModel.content;
    self.mvpView.delegate = self;
}
#pragma mark - 代理
-(void)btClicked:(UIButton *)bt{
    self.mvpModel.content = @"123456";
    self.mvpView.label.text = self.mvpModel.content;
}

在控制器中使用

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.mvpView = [[MVPView alloc]initWithFrame:self.view.bounds];
    [self.view addSubview:_mvpView];
    
    self.mvpModel = [[MVPModel alloc]init];
    
    self.presenter = [[Presenter alloc]init];
    self.presenter.mvpModel = _mvpModel;
    self.presenter.mvpView = _mvpView;
    [self.presenter presentViewWithContent];
}

大概的思路和原理就是这样的,可以根据自己的实际需求在此基础上进行整改即可.

相关文章

  • Controller瘦身

    在我们使用传统的MVC设计模式的时候,通常写着写着C中的代码就会变得又多又乱,今天通过一个简单的实例来了解MVP设...

  • Controller 的瘦身

    讨论下Controller 瘦身.在此之前一直想把tableview的代理在controller中干掉 ,可是一直...

  • MVVM与Controller瘦身实践

    前言 MVC是一个做iOS开发都知道的设计模式,也是Apple官方推荐的设计模式。实际上,Cocoa Touch就...

  • iOS - Controller 瘦身简析

    在《iOS 常见架构一览》中提到, 由于iOS 开发模式中没有在设计上规范的子组件所在位置,若使用不当,会导致UI...

  • 如何给View Controller"瘦身"

    objc.io 是一个非常有名的 iOS 开发博客,它上面的第一课 《Lighter View Controlle...

  • Swift3.0 Controller瘦身

    缘由 iOS开发,我们常用的组件UITableView,UICollectionView,通过代理的方式实现数据的...

  • 如何将controller瘦身?

    前阵子,看到了一篇关于将controller瘦身的文章,很多同学则是一直都是将数据请求放在controlle...

  • 关于controller的瘦身计划

    偶然在objc.io中看过一篇关于controller瘦身的文章.之后又从唐大神的公众号那了解到了一些给VC减肥的...

  • iOS 开发中为controller 瘦身

    在以往的开发中,使用MVC架构模式的时候还是居多的,MVC方便的是职责比较明确,View负责界面的展示,Model...

  • [转载]-MVVM与Controller瘦身实践

    收录一篇很有料的的文章,相信大家一样可以获得很多启发! [MVVM与Controller瘦身实践]http://w...

网友评论

    本文标题:Controller瘦身

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