其他设计模式
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