【发布时间】:2014-08-11 22:50:55
【问题描述】:
我的应用出现了一些随机崩溃(尽管在执行相同步骤时无法重现)。我正在观察滚动视图的 contentOffset 属性,以便在它发生变化时采取一些措施。
但是我的以下 KVO 注册和注销代码(随机)低于异常。
这里有没有可以应用的安全检查。
*** Terminating app due to uncaught exception 'NSRangeException', reason: 'Cannot remove an observer <MyPagingController 0x1f05e460> for the key path "contentOffset" from <UIScrollView 0x1f0a8fd0> because it is not registered as an observer.'
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self.scrollView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
}
- (void)viewWillDisappear:(BOOL)iAnimated {
[super viewWillDisappear:iAnimated];
[self.scrollView removeObserver:self forKeyPath:@"contentOffset"];
}
【问题讨论】:
标签: ios objective-c cocoa-touch key-value-observing