我在控制器中有滾動視圖。滾動視圖有一個子視圖。子視圖同時是滾動視圖的觀察者。當子視圖willMoveToSuperview:
調用時,我刪除觀察者。但是當控制器解散時,應用程序崩潰了。下面是範例代碼:刪除KVO觀察者時APP崩潰
@interface MyView : UIView
@property (nonatomic, weak) UIScrollView *scrollView;
@end
@implementation MyView
- (instancetype)initWithFrame:(CGRect)frame scrollView:(UIScrollView *)scrollView {
self = [super initWithFrame:frame];
if (self) {
self.scrollView = scrollView;
[scrollView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil];
}
return self;
}
- (void)willMoveToSuperview:(UIView *)newSuperview {
[super willMoveToSuperview:newSuperview];
if (!newSuperview) {
[self.scrollView removeObserver:self forKeyPath:@"contentOffset"];
self.scrollView = nil;
}
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
}
@end
@interface SecondViewController()
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
scrollView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:scrollView];
MyView *view = [[MyView alloc] initWithFrame:CGRectMake(100, 200, 100, 100) scrollView:scrollView];
[scrollView addSubview:view];
}
@end
當我在willMoveToSuperview
打印self.scrollView
,它顯示爲空。當我將MyView中的屬性scrollView
更改爲unsafe_unretained時,應用程序不會崩潰。 所以我很困惑。爲什麼不弱scrollView
工作。我是否在讀取懸掛指針時scrollView
是unsafe_unretained?這種情況有更好的解決辦法嗎?
哇是墜機? EXC_BAD_ACCESS? –
在@try中刪除代碼catch – Nick
@AndreyChernukha崩潰與未調用'removeObserver:forKeyPath:' '2016-06-12 22:39:53.752 ScrollView [20987:7043889] ***終止應用,原因是未捕獲的異常'NSInternalInconsistencyException',原因:'UIScrollView類的實例0x7fd01b824400被釋放,而鍵值觀察者仍在註冊它。現有的觀測信息:( 的語境:爲0x0,屬性:> )」 ' –
Bing