UITextView was deallocated while key value observers were still registered with it.

Trapped uncaught exception 'NSInternalInconsistencyException', reason: 'An instance 0x146028200 of class UITextView was deallocated while key value observers were still registered with it. Current observation info: (

, Key path: text, Options: , Old: NO, Prior: NO> Context: 0x0, Property: 0x170842730>

今天突然报了一个错,检查之后才发现在 >= iOS11版本的时候没问题,在

我对UITextView增加了一个KVO监听@"text"的变化,但是没有在dealloc里面销毁。

这就导致了UITextView已经被销毁了,但是我的KVO监听还一直存在。就导致崩溃了。在dealloc里面把这个监听移除就行了。

代码:

UITextView *textView = [[UITextView alloc] init];
self.textView = textView;
[textView addObserver:self forKeyPath:@"text" options:NSKeyValueObservingOptionNew context:nil];
[self.view addSubview:textView];
-(void)dealloc{
    [self.textView removeObserver:self forKeyPath:@"text"];
}

PS:只要是KVO操作,在低于iOS11版本的时候都会有问题,记得dealloc一下哦!

你可能感兴趣的:(浅析iOS,Objective-C)