通过KVO观察MKMapView的区域?
我有一个对象有兴趣了解 MKMapView 的区域何时更改。但是,该对象不是地图视图的委托。我正在尝试以下操作,其中 map
是 MKMapView:
[map addObserver:self forKeyPath:@"region" options:0 context:nil];
但是,observeValueForKeyPath:ofObject:change:context:
不会被回调。
作为临时解决方案,我让地图的委托让其他对象知道地图区域何时更改,但我想将这两个对象分开,因为它们实际上并不相关。
I have an object that is interested in knowing when the region of a MKMapView is changed. This object is not the delegate of the map view, however. I'm trying the following, where map
is a MKMapView:
[map addObserver:self forKeyPath:@"region" options:0 context:nil];
However, observeValueForKeyPath:ofObject:change:context:
isn't being called back.
As an interim solution, I have the map's delegate letting this other object know when the map region is changed, but I'd like to uncouple the two objects as they aren't really related.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 Cocoa (Touch) 中,只有如果文档这么说,框架对象的属性才保证符合 KVO。
-[MKMapView Region]
的文档没有做出这样的声明,因此您不应该尝试在其上使用 KVO。即使它确实有效,你也无法保证完全合规或持续成功。相反,您必须使用委托方法并从那里向其他对象发送消息。也许您的委托可以广播
NSNotification
来实现与 KVO 类似的效果。In Cocoa (Touch), properties of framework objects are only guaranteed to be KVO-compliant if the documentation says so. The docs for
-[MKMapView region]
make no such claim, so you shouldn't try to use KVO upon it. Even if it happened to work, you'd have no guarantee of complete compliance, or of continued success.Instead, you'll have to use the delegate method and message other objects from there. Possibly your delegate could broadcast an
NSNotification
to achieve a similar effect to KVO.