iOS底层系列之<53>--MVP(OC版)

1、MVP 主持人模式

1、ViewController
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController


@end
#import "ViewController.h"
#import "JHPresenter.h"
#import "JView.h"

@interface ViewController ()<JHViewDelegate>
@property (strong, nonatomic) JHPresenter *presenter;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.presenter = [[JHPresenter alloc] init];
    self.presenter.vc = self;
    [self.presenter setUp];
}

- (void)appViewDidClick:(JView *)jview {
    NSLog(@"点击了--");
}

@end
2、Presenter
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface JHPresenter : NSObject
@property (weak, nonatomic) UIViewController *vc;
- (void)setUp;
@end

NS_ASSUME_NONNULL_END
#import "JHPresenter.h"
#import "JView.h"
#import "Model.h"

@implementation JHPresenter

- (void)setUp {
    JView *jview = [[JView alloc] init];
    jview.delegate = self.vc;
    jview.frame = CGRectMake(100, 100, 100, 100);
    [self.vc.view addSubview:jview];

    Model *model = [[Model alloc] init];
    model.name = @"张三";
    model.imageName = @"123";

    jview.iconV.image = [UIImage imageNamed:model.imageName];
    jview.nameL.text = model.name;
    
    NSLog(@"--");
}
@end
3、View
#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN
@class JView;

@protocol JHViewDelegate <NSObject>

- (void)appViewDidClick:(JView *)jview;

@end

@interface JView : UIView
@property (weak, nonatomic) UIImageView *iconV;
@property (weak, nonatomic) UILabel *nameL;
@property (weak, nonatomic)id<JHViewDelegate> delegate;
@end

NS_ASSUME_NONNULL_END

#import "JView.h"


@implementation JView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        UIImageView *iconV = [[UIImageView alloc] init];
        iconV.frame = CGRectMake(0, 0, 100, 100);
        [self addSubview:iconV];
        _iconV = iconV;
        
        UILabel *nameL = [[UILabel alloc] init];
        nameL.frame = CGRectMake(0, 100, 100, 30);
        nameL.textAlignment = NSTextAlignmentCenter;
        [self addSubview:nameL];
        _nameL = nameL;
    }
    
    return self;
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    if ([self.delegate respondsToSelector:@selector(appViewDidClick:)]) {
        [self.delegate appViewDidClick:self];
    }
}
@end
4、Model
#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface Model : NSObject
@property (copy, nonatomic) NSString *imageName;
@property (copy, nonatomic) NSString *name;
@end

NS_ASSUME_NONNULL_END

#import "Model.h"

@implementation Model

@end

Presenter还可以改成

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface JHPresenter : NSObject
- (instancetype)initWithController:(UIViewController *)controller;
@end

NS_ASSUME_NONNULL_END

#import "JHPresenter.h"
#import "JView.h"
#import "Model.h"

@interface JHPresenter()
@property (weak, nonatomic) UIViewController *vc;
@end

@implementation JHPresenter
- (instancetype)initWithController:(UIViewController *)controller {
    if (self = [super init]) {
        self.vc = controller;
        JView *jview = [[JView alloc] init];
        jview.delegate = self.vc;
        jview.frame = CGRectMake(100, 100, 100, 100);
        [self.vc.view addSubview:jview];

        Model *model = [[Model alloc] init];
        model.name = @"张三";
        model.imageName = @"123";

        jview.iconV.image = [UIImage imageNamed:model.imageName];
        jview.nameL.text = model.name;
    }
    return self;
}


@end

ViewController使用的时候就一行代码

- (void)viewDidLoad {
    [super viewDidLoad];
    self.presenter = [[JHPresenter alloc] initWithController:self];
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值