Aar*_*ron 4 cocoa-touch dealloc ios observer-pattern automatic-ref-counting
我正在使用这堂课:
https://github.com/alexleutgoeb/ALPickerView
由于我转换为ARC,因此在点击pickerview几次后出现此错误:
2011-10-18 14:10:19.424 MappingApp[3398:10d03] An instance 0x73c7cd0 of class CustomTapGestureRecognizer was deallocated while key value observers were still registered with it. Observation info was leaked, and may even become mistakenly attached to some other object. Set a breakpoint on NSKVODeallocateBreak to stop here in the debugger. Here's the current observation info: <NSKeyValueObservationInfo 0x5d93430> (<NSKeyValueObservance 0x5d933f0: Observer: 0x5d66eb0, Key path: state, Options: <New: YES, Old: NO, Prior: NO> Context: 0x0, Property: 0x746b180>)
Run Code Online (Sandbox Code Playgroud)
该错误指向CustomTapGestureRecoginizer类和此方法的最后一行:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
// Simple hack, set recognizer state to possible as tap begins
self.state = UIGestureRecognizerStatePossible;
}
Run Code Online (Sandbox Code Playgroud)
在checkview中,我有这个方法:
- (void)didMoveToSuperview {
gestureRec = [[CustomTapGestureRecognizer alloc] initWithTarget:nil action:nil];
gestureRec.numberOfTapsRequired = 1;
[gestureRec addObserver:self forKeyPath:@"state" options:NSKeyValueObservingOptionNew context:nil];
[[self superview] addGestureRecognizer:gestureRec];
}
Run Code Online (Sandbox Code Playgroud)
而且,我知道可能导致此问题的removeObserver在checkview的dealloc中.我应该把它移到其他地方吗?有没有人有任何其他想法可能导致这个问题?它从未发生在ARC之前.
我猜想,该方法didMoveToSuperview的CheckView类被称为不止一次引起的gestureRec重新分配实例变量和以前的CustomTapGestureRecognizer实例认为没有通过ARC留下引用,然后可以dealloced(引起报警,有人还在观察实例) .
尝试添加NSLog(@"didMoveToSuperview: self=%@ gestureRec=%@", self, gestureRec);到开头,didMoveToSuperview看看是否是这种情况.
如果是这样,快速修复可能是这样的,但我自己没有尝试过或者对代码了解很多.
- (void)didMoveToSuperview {
if (gestureRec != nil) {
[gestureRec removeObserver:self forKeyPath:@"state"];
}
gestureRec = [[CustomTapGestureRecognizer alloc] initWithTarget:nil action:nil];
gestureRec.numberOfTapsRequired = 1;
[gestureRec addObserver:self forKeyPath:@"state" options:NSKeyValueObservingOptionNew context:nil];
[[self superview] addGestureRecognizer:gestureRec];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10293 次 |
| 最近记录: |