在我的UIScrollView子類中,我觀察幀之間的變化:這個observeValueForKeyPath有什麼問題:ofObject:change:context:implementation?
[self addObserver:self forKeyPath:@"frame" options:0 context:NULL];
我observeValueForKeyPath:ofObject:change:context:
實現如下:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if (object == self && [keyPath isEqualToString:@"frame"]) {
[self adjustSizeAndScale];
}
if ([UIScrollView instancesRespondToSelector:@selector(observeValueForKeyPath:ofObject:change:context:)]) {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; // Exception
}
}
,但我得到的異常與此代碼:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '<WLImageScrollView: 0x733a440; baseClass = UIScrollView; frame = (0 0; 320 416); clipsToBounds = YES; layer = <CALayer: 0x7346500>; contentOffset: {0, 0}>: An -observeValueForKeyPath:ofObject:change:context: message was received but not handled.
Key path: frame
Observed object: <WLImageScrollView: 0x733a440; baseClass = UIScrollView; frame = (0 0; 320 416); clipsToBounds = YES; layer = <CALayer: 0x7346500>; contentOffset: {0, 0}>
Change: {
kind = 1;
}
Context: 0x0'
這是否意味着UIScrollView執行observeValueForKeyPath:ofObject:change:context:
但引發上述異常?
如果是這樣,我該如何正確實施observeValueForKeyPath:ofObject:change:context:
,這樣我才能處理我感興趣的變化,並給超類有機會處理它感興趣的變化?
你掛在談論另一件事的文章:超沒有實現observeValueForKeyPath:ofObject變化:背景:。但是,從代碼中可以看出,UIScrollView的確實現了它,否則超級方法將不會被調用。 – an0
即使它確實實現了該方法,它可能會有代碼檢測未被發現的鍵併爲它們拋出異常,從而導致「NSInternalInconsistency」異常「收到但未處理」,這與發送給實例的「NSInvalidArgumentException」不可識別的選擇器不同」。 –
這就是我要問的。因此,在NSObject中的observableValueForKeyPath:ofObject:change:context:的默認實現會拋出異常?如果是這樣,我們無法檢測超類是否要通知關鍵的價值變化,不是嗎? – an0