废话只说一句,直接进主题:额。。。
@interface HYTOrderCommentViewController ()<UIScrollViewDelegate>
@property (nonatomic,weak)UIScrollView *scrollView;
@end
@implementation HYTOrderCommentViewController
-(UIScrollView *)scrollView {
if (_scrollView == nil) {
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
scrollView.delegate = self;
[self.view addSubview:scrollView];
_scrollView = scrollView;
}
return _scrollView;
}
#pragma mark -scrollViewDelegate 代理
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
[self.view endEditing:YES];
}
由于项目需要,在项目中使用了scrollview,并把delegate 设置为self;
随后 navigationcontroller pop时 HYTOrderCommentViewController销毁,在HYTOrderCommentViewController界面还未从屏幕(系统默认是左向右滑出屏幕)滑出时快速的上下拉动屏幕(根控制器是tableViewController,可以拉动),程序crash。
报错 EXC_BAD_ACCESS,野指针错误。
添加NSZombieEnabled 和 MallocStackLogging (用完记得删除)
重复运行程序,crash后会报错
*** -[HYTOrderCommentViewController scrollViewDidScroll:]: message sent to deallocated instance 0x7fe256cb0200
大概意思是调用了已经释放的HYTOrderCommentViewController对象的scrollViewDidScroll方法;
可见,使用scrollview的代理并不是很安全,需要在使用后 dealloc 方法里置空 (nil);
- (void)dealloc {
self.scrollView.delegate = nil;
NSLog(@"dealloc------死了好多次啦");
}