- objective-c - iOS 5 : Can you override UIAppearance customisations in specific classes?
- iphone - 如何将 CGFontRef 转换为 UIFont?
- ios - 以编程方式关闭标记的信息窗口 google maps iOS
- ios - Xcode 5 - 尝试验证存档时出现 "No application records were found"
在我的 UIScrollView 子类中,我正在观察框架变化:
[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:
以便我既可以处理我感兴趣的更改,又可以让父类(super class)有机会处理它感兴趣的更改?
最佳答案
添加观察者时应添加一个context
值。在您的 -observeValueForKeyPath
方法中,检查上下文参数。如果它不是您在添加观察者时传递的上下文,那么您就知道此消息不是针对您的子类的,您可以安全地将它传递给父类(super class)。如果它是相同的值,那么您知道它是为您准备的,您不应该将它传递给 super。
像这样:
static void *myContextPointer;
- (void)addSomeObserver {
[self addObserver:self forKeyPath:@"frame" options:0 context:&myContextPointer];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if (context != &myContextPointer) {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
else {
// This message is for me, do whatever I want with it.
}
}
关于ios - 这个 observeValueForKeyPath 有什么问题 :ofObject:change:context: implementation?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6574714/
我有一个对象,它实现了名为 contents 的键的索引访问器方法。在这些访问器中,当我修改底层数组时,我会调用 willChange:valuesAtIndexes:forKey: 和 didCha
一段时间以来,我一直在努力理解 ofFloat、ofInt、 和 ofObject 之间的区别。我的一部分挣扎是我不知道什么时候使用每一个。我尝试将 ofInt 用于 translationY 属性,
我想知道这个方法的参数会返回什么。 - (void) observeValueForKeyPath:(NSString *)keyPath ofObjec
在我的 UIScrollView 子类中,我正在观察框架变化: [self addObserver:self forKeyPath:@"frame" options:0 context:NULL];
我正在尝试将 Swift 2.2 项目转换为 Swift 3,但出现以下错误: Method 'observeValue(forKeyPath:ofObject:change:context:)' w
我对 KVO 比较陌生,所以我很可能违反了一些基本规则。我正在使用核心数据。 我的应用程序崩溃并显示以下消息:我无法理解为什么 CGImage 参与观察在MeasurementPointer 对象上设
我想根据内容动态设置 Collection View 的高度,我尝试了以下代码: override func viewWillAppear(_ animated: Bool) {
我通过调用以下方法来观察 AVPlayer 的 rate 属性的变化: addObserver:sharedPlayerManagerInstance forKeyPath:@"rate"
因此,我将此文本字段 someField 绑定(bind)到 InterfaceBuilder (IB) 中的属性 someText。在我的代码中的任何位置,当我更改 someText 时,someF
我有一个 NSOperation 子类,它实现 setFinished: 来生成 KVO 通知: -(void)setFinished:(BOOL)isFinished { LogError(
我是一名优秀的程序员,十分优秀!