ios:observeValueForKeyPath后崩溃

ios:observeValueForKeyPath后崩溃,ios,ipad,Ios,Ipad,我在我的代码中发现了一个奇怪的bug,但我不能在任何时候复制它。 有时,我的iPad应用程序会因以下关键路径错误而崩溃: Keypath contentSize changed *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '<UIWebDocumentView: 0x1c9fce00; frame = (0 0; 1034 75); text = 'co

我在我的代码中发现了一个奇怪的bug,但我不能在任何时候复制它。 有时,我的iPad应用程序会因以下关键路径错误而崩溃:

Keypath contentSize changed 
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '<UIWebDocumentView: 0x1c9fce00; frame = (0 0; 1034 75); text = 'coucou'; opaque = NO; userInteractionEnabled = NO; layer = <UIWebLayer: 0x1daa9760>>: An -observeValueForKeyPath:ofObject:change:context: message was received but not handled.
Key path: contentSize
Observed object: <UITextView: 0x1da83e60; frame = (32 32; 1034 198); text = 'coucou'; layer = <CALayer: 0x1dad2650>; contentOffset: {0, -62}>
Change: {
kind = 1;
new = "NSSize: {1034, 75}";
}
这里是我附上它的地方:

UITextView *tv = [[UITextView alloc] initWithFrame:button.frame]; 
[tv addObserver:self forKeyPath:@"contentSize" options:(NSKeyValueObservingOptionNew) context:(void *)selected];
textViewCurrentlyEditing = tv;
“选定”是UIObject类型

我的代码中没有使用UIWebLayer。 为什么从UITextView到UIWebLayer会发生objet突变? 我在干什么

试试这个:

在.h文件即@interface文件中,声明UITextView*tv; 然后在需要的地方写下这段代码

if(tv)[tv release];
tv = [[UITextView alloc] initWithFrame:button.frame]; 
此方法将始终帮助您避免陷入任何内存损坏问题。
我只希望它能帮你。祝你好运

我想我可能已经在这篇文章中找到了解决办法:

我在“removeFromSuperview”之前添加了“removeObserver”


你忘了在dealloc中注销textview吗?我的textview并没有真正注销,因为我使用的是ARC。当我不再需要它时,我只需要:[tv removeFromSuperview];tv=零;Instruments没有检测到任何僵尸,但我猜如果奇怪的textview在某个地方仍然存在,它还不是僵尸。我正在使用ARC,无法手动释放对象。不过,在再次分配之前,我将其分配为nil,希望它能有所帮助
if(tv)[tv release];
tv = [[UITextView alloc] initWithFrame:button.frame]; 
    [tv removeObserver:self forKeyPath:@"contentSize"];