-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNSObject+KVO.m
More file actions
159 lines (127 loc) · 5.53 KB
/
NSObject+KVO.m
File metadata and controls
159 lines (127 loc) · 5.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//
// NSObject+KVO.m
// KVO
//
// Created by mac on 2018/3/19.
// Copyright © 2018年 mac. All rights reserved.
//
#import "NSObject+KVO.h"
#import <objc/runtime.h>
#import <UIKit/UIKit.h>
static NSString *handlerDicName = @"handlerDicName";
static NSString *funcHandlerDicName = @"funcHandlerDicName";
static NSString *notificationHandlerDicName = @"notificationHandlerDicName";
static NSString *textHandlerName = @"textHandlerName";
@interface NSObject ()<UITextViewDelegate>
@property(nonatomic,strong)NSMutableDictionary * handlerDic;
@property(nonatomic,strong)NSMutableDictionary * funcHandlerDic;
@property(nonatomic,strong)NSMutableDictionary * notificationHandlerDic;
@property(nonatomic,copy)TextHandler tHandler;
@end
@implementation NSObject (KVO)
#pragma mark -添加kvo
-(void)addObserverForKeyPath:(NSString *)keyPath complete:(CompleteHandler)completeHandler{
if (!self.handlerDic) {
NSMutableDictionary * dic = [[NSMutableDictionary alloc] init];
self.handlerDic = dic;
}
[self.handlerDic setObject:completeHandler forKey:keyPath];
[self addObserver:self forKeyPath:keyPath options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:NULL];
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
CompleteHandler handler = self.handlerDic[keyPath];
if (handler) {
handler(change[@"new"], change[@"old"]);
}
}
- (NSMutableDictionary *)handlerDic{
return objc_getAssociatedObject(self, &handlerDicName);
}
- (void)setHandlerDic:(NSMutableDictionary *)handlerDic{
objc_setAssociatedObject(self, &handlerDicName, handlerDic,OBJC_ASSOCIATION_RETAIN);
}
#pragma mark -事件监听
-(void)addFuncObserverForSelector:(NSString *)selector withFuncHandler:(FuncHandler)funcHandler{
if (!self.funcHandlerDic) {
NSMutableDictionary * dic = [[NSMutableDictionary alloc] init];
self.funcHandlerDic = dic;
}
[self.funcHandlerDic setObject:funcHandler forKey:selector];
Method method = class_getInstanceMethod([self class], NSSelectorFromString(selector));
NSString *exc_str = [NSString stringWithFormat:@"func_%@",selector];
class_addMethod([self class], NSSelectorFromString(exc_str), (IMP)exc_funcMethod, "v@:");
Method exc_method = class_getInstanceMethod([self class], NSSelectorFromString(exc_str));
method_exchangeImplementations(method, exc_method);
}
void exc_funcMethod(id self, SEL _cmd, NSString *param){
NSObject *obj = self;
FuncHandler handler = obj.funcHandlerDic[NSStringFromSelector(_cmd)];
if (handler) {
handler(param);
}
SEL selector = NSSelectorFromString([NSString stringWithFormat:@"func_%@",NSStringFromSelector(_cmd)]);
[self performSelector:selector withObject:param];
}
- (NSMutableDictionary *)funcHandlerDic{
return objc_getAssociatedObject(self, &funcHandlerDicName);
}
- (void)setFuncHandlerDic:(NSMutableDictionary *)funcHandlerDic{
objc_setAssociatedObject(self, &funcHandlerDicName, funcHandlerDic,OBJC_ASSOCIATION_RETAIN);
}
#pragma mark -通知监听
-(void)addNotificationObserverForName:(NSString *)name object:(id)object withNotificationHandler:(NotificationHandler)notificationHandler{
if (![self isKindOfClass:[NSNotificationCenter class]]) {
@throw [NSException exceptionWithName:@"NotificationObserverError" reason:@"obj isn't NSNotificationCenter" userInfo:nil];
}
if (!self.notificationHandlerDic) {
NSMutableDictionary * dic = [[NSMutableDictionary alloc] init];
self.notificationHandlerDic = dic;
}
[self.notificationHandlerDic setObject:notificationHandler forKey:name];
[(NSNotificationCenter *)self addObserver:self selector:@selector(notiAcion:) name:name object:object];
}
-(void)notiAcion:(NSNotification *)noti{
NotificationHandler handler = self.notificationHandlerDic[noti.name];
if (handler) {
handler(noti);
}
}
- (NSMutableDictionary *)notificationHandlerDic{
return objc_getAssociatedObject(self, ¬ificationHandlerDicName);
}
- (void)setNotificationHandlerDic:(NSMutableDictionary *)notificationHandlerDic{
objc_setAssociatedObject(self, ¬ificationHandlerDicName, notificationHandlerDic,OBJC_ASSOCIATION_RETAIN);
}
#pragma mark - textField.text or textView.text change
-(void)addTextObserverWithTextHandler:(TextHandler)textHandler{
if (![self isKindOfClass:[UITextField class]] && ![self isKindOfClass:[UITextView class]]) {
@throw [NSException exceptionWithName:@"TextObserverError" reason:@"obj isn't UITextField or UITextView" userInfo:nil];
}
if ([self isKindOfClass:[UITextField class]]) {
self.tHandler = textHandler;
UITextField *tf = (UITextField *)self;
[tf addTarget:self action:@selector(textFieldDidChange) forControlEvents:UIControlEventEditingChanged];
}else{
self.tHandler = textHandler;
UITextView *tv = (UITextView *)self;
tv.delegate = self;
}
}
-(void)textFieldDidChange{
if (self.tHandler) {
UITextField *tf = (UITextField *)self;
self.tHandler(tf.text);
}
}
-(void)textViewDidChange:(UITextView *)textView{
if (self.tHandler) {
self.tHandler(textView.text);
}
}
- (TextHandler)tHandler{
return objc_getAssociatedObject(self, &textHandlerName);
}
- (void)setTHandler:(TextHandler)tHandler{
objc_setAssociatedObject(self, &textHandlerName, tHandler,OBJC_ASSOCIATION_RETAIN);
}
@end