本文介绍了通过KVO观察MKMapView的区域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象,该对象想知道MKMapView的区域何时更改.但是,此对象不是地图视图的委托.我正在尝试以下操作,其中map是MKMapView:

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];

但是,observeValueForKeyPath:ofObject:change:context:没有被回叫.

作为一个临时解决方案,我让地图的委托人在更改地图区域时让另一个对象知道,但是我想解除这两个对象之间的联系,因为它们并没有真正的联系.

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.

推荐答案

在可可(Touch)中,仅在文档中如此说明的情况下,才保证框架对象的属性符合KVO . -[MKMapView region]的文档没有这样的声明,因此您不应尝试在其上使用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.

相反,您必须使用委托方法并从那里向其他对象发送消息.可能您的代表可以广播NSNotification以达到与KVO类似的效果.

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.

这篇关于通过KVO观察MKMapView的区域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-20 17:40