コードに奇妙なバグを見つけましたが、いつでも再現できません。時々、私の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}";
}
キーパス監視を処理するコードは次のとおりです。
-(void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if (object != textViewCurrentlyEditing)
return;
NSLog(@"Keypath %@ changed", keyPath);
UITextView *tv = object;
UIObject *selected = (__bridge UIObject *)context;
[self updateTextViewAlign:tv forObject:selected];
}
そしてここに私はそれを付けます:
UITextView *tv = [[UITextView alloc] initWithFrame:button.frame];
[tv addObserver:self forKeyPath:@"contentSize" options:(NSKeyValueObservingOptionNew) context:(void *)selected];
textViewCurrentlyEditing = tv;
「選択済み」はUIObjectタイプです。
コードのどこにもUIWebLayerを使用していません。UITextViewからUIWebLayerへのオブジェクトの変更があるのはなぜですか?私は何をしているのですか?