iOS 懒加载在中的奇葩内存泄漏
2019-02-22 本文已影响0人
YannChee
原来代码
// 有内存泄漏
- (QYSegmentedView *)segmentBar {
if (!_segmentBar) {
QYSegmentedView *segmentBar = [QYSegmentedView segmentedViewWithFrame:CGRectZero];
segmentBar.backgroundColor = [UIColor brownColor];
[self.view addSubview:segmentBar];
segmentBar.didSlectedIndexBlock = ^(NSInteger targetIndex, NSInteger sourceIndex) {
[self showChildVCViewsAtIndex:targetIndex];
};
_segmentBar = segmentBar;
}
return _segmentBar;
}
// 无内存泄漏
- (QYSegmentedView *)segmentBar {
if (!_segmentBar) {
QYSegmentedView *segmentBar = [QYSegmentedView segmentedViewWithFrame:CGRectZero];
segmentBar.backgroundColor = [UIColor brownColor];
[self.view addSubview:segmentBar];
__weak __typeof(self)weakSelf = self;;
segmentBar.didSlectedIndexBlock = ^(NSInteger targetIndex, NSInteger sourceIndex) {
[weakSelf showChildVCViewsAtIndex:targetIndex];
};
_segmentBar = segmentBar;
}
return _segmentBar;
}