通过KVO观察MKMapView的区域?

发布于 2024-10-06 04:31:57 字数 332 浏览 2 评论 0原文

我有一个对象有兴趣了解 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

街角卖回忆 2024-10-13 04:31:57

在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文