Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Observing the region of MKMapView via KVO?

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.

like image 684
James J Avatar asked Dec 06 '10 19:12

James J


1 Answers

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.

like image 158
Mike Abdullah Avatar answered Sep 21 '22 06:09

Mike Abdullah