Skip to content

Commit

Permalink
避免循环引用导致释放不了的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
WZBbiao committed Jan 4, 2017
1 parent 3573ffc commit c1d8a9f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 18 deletions.
15 changes: 12 additions & 3 deletions WZBTextView-demo/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ - (void)test1 {
self.textView.hidden = NO;
self.textView.placeholder = @"i love you";
self.textView.maxHeight = 100.05;

}

- (void)test2 {
Expand All @@ -38,12 +39,16 @@ - (void)test2 {
textView.placeholder = @"i love you";
textView.layer.borderColor = [UIColor lightGrayColor].CGColor;
textView.layer.borderWidth = 1;

// 避免循环引用
__weak typeof (self) weakSelf = self;
__weak typeof (textView) weakTextView = textView;
[textView autoHeightWithMaxHeight:100 textViewHeightDidChanged:^(CGFloat currentTextViewHeight) {
CGRect frame = textView.frame;
CGRect frame = weakTextView.frame;
frame.size.height = currentTextViewHeight;
[UIView animateWithDuration:0.2 animations:^{
textView.frame = frame;
textView.center = self.view.center;
weakTextView.frame = frame;
weakTextView.center = weakSelf.view.center;
}];
}];
self.tView = textView;
Expand All @@ -54,6 +59,10 @@ - (void)test2 {
[button addTarget:self action:@selector(addImage) forControlEvents:UIControlEventTouchUpInside];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self.textView resignFirstResponder];
}

- (void)addImage {
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypePhotoLibrary] || [UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeSavedPhotosAlbum]) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
Expand Down
40 changes: 25 additions & 15 deletions WZBTextView-demo/WZBTextView/UITextView+WZB.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,31 @@ @interface UITextView ()

@implementation UITextView (WZB)

#pragma mark - Swizzle Dealloc

+ (void)load {
// 交换dealoc
Method dealoc = class_getInstanceMethod(self.class, NSSelectorFromString(@"dealloc"));
Method myDealoc = class_getInstanceMethod(self.class, @selector(myDealoc));
method_exchangeImplementations(dealoc, myDealoc);
}

- (void)myDealoc {
// 移除监听
[[NSNotificationCenter defaultCenter] removeObserver:self];

UITextView *placeholderView = objc_getAssociatedObject(self, WZBPlaceholderViewKey);

// 如果有值才去调用,这步很重要
if (placeholderView) {
NSArray *propertys = @[@"frame", @"bounds", @"font", @"text", @"textAlignment", @"textContainerInset"];
for (NSString *property in propertys) {
[self removeObserver:self forKeyPath:property];
}
}
[self myDealoc];
}

- (UITextView *)placeholderView {

// 为了让占位文字和textView的实际文字位置能够完全一致,这里用UITextView
Expand Down Expand Up @@ -233,19 +258,4 @@ - (void)addImage:(UIImage *)image size:(CGSize)size index:(NSInteger)index multi

}

- (void)dealloc {
// 移除监听
[[NSNotificationCenter defaultCenter] removeObserver:self];

UITextView *placeholderView = objc_getAssociatedObject(self, WZBPlaceholderViewKey);

// 如果有值才去调用,这步很重要
if (placeholderView) {
NSArray *propertys = @[@"frame", @"bounds", @"font", @"text", @"textAlignment", @"textContainerInset"];
for (NSString *property in propertys) {
[self removeObserver:self forKeyPath:property];
}
}
}

@end

0 comments on commit c1d8a9f

Please sign in to comment.