Issue
When using the MapKit library and placing a pin/MKAnnotation, you will implement the MKAnnotation class having the coordinates, name and description:
@interface MyAnnotationClass : NSObject <MKAnnotation> {
NSString *_name;
NSString *_description;
CLLocationCoordinate2D _coordinate;
}
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *description;
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
-(id) initWithCoordinate:(CLLocationCoordinate2D) coordinate;
@end
These core details are displayed when a pin is placed on the map. However, when you want to have that same pin have a button, you implemente the following method in the MapViews Delegate:
-(MKAnnotationView *)mapView:(MKMapView *)mapview viewForAnnotation:(id <MKAnnotation>)annotation {
...
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
return(annotationView);
}
Why is this the convention? I would expect that the 'rightCalloutAccessory' would be a part of MKAnnotation. I know that this is optional, so could it be that the rightCallout object is part of a more specific method?
It is confusing to me because you are setting the attributes of a Pin when creating the MKAnnotation object, but then setting other attributes when the pin is being placed on the map. Why arent both of these done in the same place? Is it possible to do it that way?
Solution
Usually the MKAnnotation
protocol is implemented by model objects. The model layer isn't supposed to know anything about views. So to honor the MVC pattern the callout view can not be part of the MKAnnotation
object.
Answered By - Sven
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.