MVVM 自研之 view绑定 model

本文介绍了一个用于iOS开发的视图绑定工具,该工具通过绑定不同的数据模型(如直播间模型和用户模型)到视图上,并在模型数据更新时自动刷新UI。通过简单的设置,可以实现对多个视图进行统一管理及数据同步。
#import <Foundation/Foundation.h>

typedef NS_ENUM(NSInteger, QIEBindViewType) {
    QIEBindViewTypeRoomModel,   /**< 绑定直播间 model */
    QIEBindViewTypeUserModel    /**< 绑定用户 model */
};

/** 更新 UI 时候需要调用的方法 */
@protocol QIERefreshUIProtocol <NSObject>

/** 更新 UI  viewModel: 变化的 model */
-(void)refreshUI:(id)viewModel;

@end

@interface QIEBindModelViewTool : NSObject

/** 绑定的直播间 model */
@property (nonatomic, strong) DYRoomModel *roomModel;

/** 绑定的用户 model */
@property (nonatomic, strong) QIEUserModel *userModel;

/** 单例初始化 */
+(instancetype)bindModelViewTool;

/** 当调用 bindViewType 对应的 model 的 set方法 就调用 anObj 的 refreshUI: 方法*/
-(void)bindModelClass:(QIEBindViewType)bindViewType object:(id <QIERefreshUIProtocol> )anObj;

/** 销毁工具类 */
+(void)destoryTool;

@end

@interface QIEBindModelViewTool ()

/** 绑定直播间 model 的 view 数组 */
@property (nonatomic, strong) NSHashTable <id <QIERefreshUIProtocol>> *bindRoomModelViewsArray;

/** 绑定用户信息 model 的 view 数组 */
@property (nonatomic, strong) NSHashTable <id <QIERefreshUIProtocol>> *bindUserModelViewsArray;

@end

@implementation QIEBindModelViewTool

// - MARK: <-- 初始化 -->
static QIEBindModelViewTool *bindModelViewTool_ = nil;
+(instancetype)bindModelViewTool{
    if (!bindModelViewTool_) {
        bindModelViewTool_ = [[QIEBindModelViewTool alloc]init];
    }
    return bindModelViewTool_;
}

/** 当 aClass 的对象发生变化时候 就调用 anObj 的 refreshUI: 方法*/
-(void)bindModelClass:(QIEBindViewType)bindViewType object:(id <QIERefreshUIProtocol> )anObj{
    
    // - 做非空的判断
    if (!anObj || (![anObj respondsToSelector:@selector(refreshUI:)])) return;
    
    // - 根据类型刷新 UI
    if (bindViewType == QIEBindViewTypeUserModel) {
        [self.bindUserModelViewsArray addObject:anObj];
        [anObj refreshUI:self.userModel];
    }else if (bindViewType == QIEBindViewTypeRoomModel){
        [self.bindRoomModelViewsArray addObject:anObj];
        [anObj refreshUI:self.roomModel];
    }
}

/** 更新 roomModel 的数据 */
-(void)setRoomModel:(DYRoomModel *)roomModel{
    _roomModel = roomModel;
    for (id <QIERefreshUIProtocol> obj in self.bindRoomModelViewsArray) {
        [obj refreshUI:roomModel];
    }
}

/** 更新 userModel 的数据 */
-(void)setUserModel:(QIEUserModel *)userModel{
    _userModel = userModel;
    for (id <QIERefreshUIProtocol> obj in self.bindRoomModelViewsArray) {
        [obj refreshUI:userModel];
    }
}

// - MARK: <-- 懒加载 -->
/** 绑定直播间 model 的 view 数组 */
-(NSHashTable <id <QIERefreshUIProtocol>> *)bindRoomModelViewsArray{
    if(!_bindRoomModelViewsArray){
        _bindRoomModelViewsArray = [NSHashTable weakObjectsHashTable];
    }
    return _bindRoomModelViewsArray;
}

/** 绑定用户信息 model 的 view 数组 */
-(NSHashTable <id <QIERefreshUIProtocol>> *)bindUserModelViewsArray{
    if(!_bindUserModelViewsArray){
        _bindUserModelViewsArray = [NSHashTable weakObjectsHashTable];
    }
    return _bindUserModelViewsArray;
}

// - MARK: <-- 释放内存 -->
/** 销毁工具类 */
+(void)destoryTool{
    if (bindModelViewTool_) {
        [bindModelViewTool_.bindRoomModelViewsArray removeAllObjects];
        [bindModelViewTool_.bindUserModelViewsArray removeAllObjects];
        bindModelViewTool_ = nil;
    }
}

-(void)dealloc{
    NSLog(@"==========dealloc=========");
}
@end

使用 : 方法如下

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(90, 90, 90, 90)];
    [self.view addSubview:label];
    label.textColor = [UIColor redColor];
    self.label = label;
    [[QIEBindModelViewTool bindModelViewTool] bindModelClass:QIEBindViewTypeRoomModel object:self];
}

-(void)refreshUI:(DYRoomModel *)viewModel{
    self.label.text = viewModel.title;
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    DYRoomModel *r = [[DYRoomModel alloc]init];
    r.title = @"哈哈哈";
    [QIEBindModelViewTool bindModelViewTool].roomModel = r;
}

git 地址 : https://github.com/lichaoqun/QGBindViewModel.git

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值