Before iOS 4, I used to add a observer to each MKAnnotationView
added to the map view, listening for it's selected method, so I know when the user has tapped on a pin.
This worked fine up to iOS 4.2. I've noticed on the release noted annotation views are actually being reused and it somehow messes up with the observers.
So I figure I can use the -mapview:didSelectAnnotationView:
method from MKMapViewDelegate
for my needs, but that has only been added to iOS 4.0 SDK.
So, to maintain compatibility, I'd like to implement this method on my delegate and conditionally check for the presence of this method on the MKMapViewDelegate
protocol so that if it's not present, I will add my observer to the annotation view.
How can I do this for a protocol method, similarly for how we check if a class is not nil?
UPDATE:
As Daniel Dickison pointed out, I can't use respondsToSelector:
, because my delegate has -mapview:didSelectAnnotationView:
implemented for 4.0+ devices. What I need is to check if the protocol on that device has the optional -mapview:didSelectAnnotationView:
method OR if MKMapView
will look for that method on it's delegate.
I ended up doing a test for the current iOS version running. If it's higher than 4.0, MKMapView
will look for that method and call it.
if ([[[UIDevice currentDevice] systemVersion] doubleValue] < 4.0)
[self setupObserver];
This solves the original problem, but it would still be interesting to check the actual protocol for the method somehow.