美文网首页
MVP模式示例代码

MVP模式示例代码

作者: 小苗晓雪 | 来源:发表于2018-08-23 10:14 被阅读20次
image.png

ViewController.m

#import "ViewController.h"
#import "MVPViewCtrl.h"


@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self showMVP];
    });
}


- (void)showMVP
{
    MVPViewCtrl *vc = [MVPViewCtrl new];
    [self presentViewController:vc animated:YES completion:nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    
}

MVPViewCtrl.h

#import <UIKit/UIKit.h>

@interface MVPViewCtrl : UIViewController

@end

MVPViewCtrl.m

#import "MVPViewCtrl.h"
#import "MVPPresenter.h"
#import "MVPView.h"
#import "MVPModel.h"
//在ctrl中

@interface MVPViewCtrl ()

@property (nonatomic, strong) MVPPresenter *mvpPresenter;
@property (nonatomic, strong) MVPModel *mvpModel;
@property (nonatomic, strong) MVPView *mvpView;


@end

@implementation MVPViewCtrl

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    // presenter:
    self.mvpPresenter = [[MVPPresenter alloc] init];
    
    // view:
    self.mvpView = [[MVPView alloc] init];
    self.mvpView.frame = CGRectMake(0, 0, 300, 500);
    self.mvpView.delegate = self.mvpPresenter;
    [self.view addSubview:self.mvpView];
    // 给present的view赋值实参:
    self.mvpPresenter.mvpView = self.mvpView;
    
    // model:
    self.mvpModel = [[MVPModel alloc] init];
    self.mvpModel.contentString = @"line 0";
    // 给present的model赋值实参:
    self.mvpPresenter.mvpModel = self.mvpModel;
    
    // 调用present的打印任务方法:
    [self.mvpPresenter printTask];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    
}

MVPPresenter.h

#import <Foundation/Foundation.h>
#import "MVPView.h"
#import "MVPModel.h"


@interface MVPPresenter : NSObject  <MVPViewDelegate>

@property (nonatomic, strong) MVPModel *mvpModel;
@property (nonatomic, strong) MVPView *mvpView;

/**
 *  打印任务:
 
 */
- (void)printTask;

@end

MVPPresenter.m

#import "MVPPresenter.h"

@implementation MVPPresenter

/**
 *  打印任务:
 
 */
- (void)printTask
{
    // 调用view展示model内容:
    [self.mvpView printOnView:self.mvpModel.contentString];
}


- (void)buttonClickedMethod
{
    int randomColor = arc4random() % 10;
    self.mvpModel.contentString = [NSString stringWithFormat:@"line %d" , (randomColor + 1)];
    [self printTask];
}

MVPView.h

#import <UIKit/UIKit.h>

@protocol MVPViewDelegate<NSObject>

- (void)buttonClickedMethod;

@end

@interface MVPView : UIView

- (void)printOnView:(NSString *)contentString;

@property (nonatomic, weak) id<MVPViewDelegate> delegate;

@end

MVPView.m

#import "MVPView.h"

@interface MVPView ()

// 内容视图
@property (nonatomic, strong) UILabel *contentLabel;


@property (nonatomic, strong) UIButton *button;

@end


@implementation MVPView

- (instancetype)init
{
    self = [super init];
    if (self)
    {
        self.backgroundColor = [UIColor yellowColor];
        self.layer.cornerRadius = 5.0f;
        self.layer.masksToBounds = YES;
        self.layer.borderColor = [UIColor redColor].CGColor;
        [self addSubview:self.contentLabel];
        [self addSubview:self.button];
    }
    return self;
}

- (void)printOnView:(NSString *)contentString
{
    self.contentLabel.text = contentString;
}


- (UILabel *)contentLabel
{
    if(!_contentLabel)
    {
        _contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, 300, 30)];
        _contentLabel.textColor = [UIColor blackColor];
        _contentLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _contentLabel;
}

- (UIButton *)button{
    if(!_button)
    {
        _button = [UIButton buttonWithType:UIButtonTypeCustom];
        [_button setTitle:@"print" forState:UIControlStateNormal];
        [_button setFrame:CGRectMake(0, 200, 100, 100)];
        _button.layer.cornerRadius = 5;
        _button.layer.masksToBounds = YES;
        _button.backgroundColor = [UIColor redColor];
        [_button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        _button.backgroundColor = [UIColor redColor];
        [_button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _button;
}

- (void)buttonClicked:(id)sender
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(buttonClickedMethod)])
    {
        [self.delegate buttonClickedMethod];
    }
}

@end

MVPModel.h

#import <Foundation/Foundation.h>

@interface MVPModel : NSObject

// 打印内容:
@property (nonatomic, copy) NSString *contentString;

@end

MVPModel.m

#import "MVPModel.h"

@implementation MVPModel

@end

愿编程让这个世界更美好

相关文章

网友评论

      本文标题:MVP模式示例代码

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