此代码来自 MapCallouts 演示。假设我有数百个不同的注释。苹果这样做的方式,会导致大量的代码重复。
我想访问触发委托的类实例的注释属性,无论哪个类实例触发它。
有没有比编写 if 语句来处理每个注释并拥有一个通用方法更简单的方法?
- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation
{
// if it's the user location, just return nil.
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
// handle our two custom annotations
//
if ([annotation isKindOfClass:[BridgeAnnotation class]]) // for Golden Gate Bridge
{
//do something
}
else if ([annotation isKindOfClass:[SFAnnotation class]]) // for City of San Francisco
{
//do something
}
return nil;
}