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];
}