编码多个 MKAnnotations 的更简单方法?
此代码来自 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;
}
This code is from the MapCallouts demo. Say I had hundreds of different annotations. The way that Apple has done it, it would result in a lot of code duplication.
I want to access the annotation properties of the instance of the class that triggered the delegate, no matter which class instance triggered it.
Is there a simpler way than writing if statements to handle each annotation and have one universal method?
- (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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以让所有注释类提供一些通用方法,例如
-annotationView
。您可以从公共超类派生所有注释类,或者只是创建一个协议。然后,检查注释是否确实响应选择器,或者是公共类的子类,并询问它的视图:或
不这样做的一个原因是您用作注释的对象通常是数据模型的一部分,它们可能与注释视图没有任何关系。能够提供标题、副标题和位置是一回事;能够提供标题、副标题和位置是一回事;能够提供标题、副标题和位置是一回事。提供视图的实际实例通常超出了模型对象应该执行的范围。
请记住,注释视图通常除了显示图片并为标注视图提供左右附件之外不会做太多事情。您真的可能需要数百个不同的注释视图子类吗?或者您可以对所有注释使用通用注释视图,并以不同的方式配置它们(例如通过更改注释视图的图像)?
You could have all your annotation classes provide some common method, like
-annotationView
. You might derive all the annotation classes from a common superclass, or just create a protocol. Then, check that the annotation actually responds to the selector, or is a subclass of your common class, and ask it for it's view:or
One reason not to do that is that the objects you use as annotations are often parts of your data model, and they may not have any business knowing anything about annotation views. It's one thing to be able to provide a title, subtitle, and location; providing an actual instance of a view is often outside the scope of what a model object should do.
Keep in mind that annotation views usually don't do too much other than display a picture and provide the left and right accessories for the callout view. Is it really likely that you'll need hundreds of different annotation view subclasses? Or can you use a common annotation view for all your annotations, and just configure them differently (such as by changing the annotation view's image)?