-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUIScrollView+safeDelegate.m
57 lines (53 loc) · 1.78 KB
/
UIScrollView+safeDelegate.m
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
//
// UIScrollView+safeDelegate.m
// ieltsListen
//
// Created by 王志平 on 2017/8/18.
// Copyright © 2017年 enhance. All rights reserved.
//
#import "UIScrollView+safeDelegate.h"
#import <objc/runtime.h>
#import "NSObject+Guard.h"
@implementation UIScrollView (safeDelegate)
+ (void)load
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class class = [self class];
SEL oldSEL = @selector(setDelegate:);
SEL newSEL = @selector(myselfSetDelegate:);
Method oldMethod = class_getInstanceMethod(class, oldSEL);
Method newMethod = class_getInstanceMethod(class, newSEL);
BOOL success = class_addMethod(class, oldSEL, method_getImplementation(newMethod), method_getTypeEncoding(newMethod));
if (success)
{
class_replaceMethod(class, newSEL, method_getImplementation(oldMethod), method_getTypeEncoding(oldMethod));
}
else
{
method_exchangeImplementations(oldMethod, newMethod);
}
});
}
- (void)myselfSetDelegate:(id )delegate
{
if (delegate) {
__weak typeof(UIScrollView *)weak_self = self;
[delegate guard_addDeallocBlock:^{
weak_self.delegate = nil;
if ([weak_self isKindOfClass:[UITableView class]])
{
((UITableView *)weak_self).editing = NO;
((UITableView *)weak_self).dataSource = nil;
((UITableView *)weak_self).delegate = nil;
}
else if ([weak_self isKindOfClass:[UICollectionView class]])
{
((UICollectionView *)weak_self).dataSource = nil;
((UICollectionView *)weak_self).delegate = nil;
}
}];
}
[self myselfSetDelegate:delegate];
}
@end