iOS底层系列之<54>--MVVM(OC版)

其他设计模式

MVVM示例

需要导入FBKVO
https://github.com/facebookarchive/KVOController
导入下面4个文件即可
在这里插入图片描述

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

@interface ViewController : UIViewController

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

@interface ViewController ()
@property (strong, nonatomic) JHViewModel *viewModel;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.viewModel = [[JHViewModel alloc] initWithController:self];
}

@end
2、JHViewModel
#import <UIKit/UIKit.h>
@class Model,JView;
NS_ASSUME_NONNULL_BEGIN
@interface JHViewModel : NSObject
- (instancetype)initWithController:(UIViewController *)controller;

@end

NS_ASSUME_NONNULL_END

#import "JHViewModel.h"
#import "JView.h"
#import "Model.h"

@interface JHViewModel()<JHViewDelegate>
@property (weak, nonatomic) UIViewController *controller;
@property (copy, nonatomic) NSString *imageName;
@property (copy, nonatomic) NSString *name;
@property (strong, nonatomic) Model *model;
@property (strong, nonatomic) JView *jview;
@property (assign, nonatomic) int count;
@end

@implementation JHViewModel

- (instancetype)initWithController:(UIViewController *)controller {
    if (self = [super init]) {
        self.controller = controller;
        JView *jview = [[JView alloc] init];
        jview.viewModel = self;
        jview.delegate = self;
        jview.frame = CGRectMake(100, 100, 100, 100);
        self.jview = jview;
        [self.controller.view addSubview:jview];
        
        
        self.model = [[Model alloc] init];
        [self setup];
    }
    return self;
}
- (void)setup {
    if (self.count % 2 == 0) {
        self.model.name = [NSString stringWithFormat:@"张三%d",self.count];
        self.model.imageName = @"123";
        
        self.name = self.model.name;
        self.imageName = self.model.imageName;
    } else {
        self.model.name = [NSString stringWithFormat:@"李四%d",self.count];
        self.model.imageName = @"345";
        
        self.name = self.model.name;
        self.imageName = self.model.imageName;
    }
}

- (void)appViewDidClick:(JView *)jview {
    
    self.count++;
    NSLog(@"点击了===%d",self.count);
    [self setup];
}

@end

2、JView
#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN
@class JView,JHViewModel;

@protocol JHViewDelegate <NSObject>

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

@end

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

NS_ASSUME_NONNULL_END
#import "JView.h"
#import "FBKVO/NSObject+FBKVOController.h"
#import "FBKVO/FBKVOController.h"
#import "JHViewModel.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)setViewModel:(JHViewModel *)viewModel {
    _viewModel = viewModel;
    
    
    __weak typeof(self) weakSelf = self;
    [self.KVOController observe:viewModel keyPath:@"name" options:NSKeyValueObservingOptionNew block:^(id  _Nullable observer, id  _Nonnull object, NSDictionary<NSKeyValueChangeKey,id> * _Nonnull change) {
        weakSelf.nameL.text = change[NSKeyValueChangeNewKey];
    }];
    
    [self.KVOController observe:viewModel keyPath:@"imageName" options:NSKeyValueObservingOptionNew block:^(id  _Nullable observer, id  _Nonnull object, NSDictionary<NSKeyValueChangeKey,id> * _Nonnull change) {
        weakSelf.iconV.image = [UIImage imageNamed:change[NSKeyValueChangeNewKey]];
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    if ([self.delegate respondsToSelector:@selector(appViewDidClick:)]) {
        [self.delegate appViewDidClick:self];
    }
}
@end
3、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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值