【发布时间】:2014-10-28 13:33:02
【问题描述】:
我有一个 MKMapView。在这个视图中,我创建了一些注释。当用户点击注释披露按钮时,我想推送到详细视图。
当我创建注释时,我还填充了一个 NSMutableDictionary 以便之后能够以该点为键来获取存储对象。
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
CLLocationCoordinate2D annotationCoord;
annotationCoord.latitude = [[store storeLatitude] floatValue];
annotationCoord.longitude = [[store storeLongitude] floatValue];
point.coordinate = annotationCoord;
NSString *title = [store storeName];
point.title = title;
point.subtitle = location;
// keep reference to store object with the annotation point as key
[annotationStoreDictionary setObject:store forKey:point];
当用户选择注解时,我想调用DetailViewController并传递对象:
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
DetailViewController *con = [storyboard instantiateViewControllerWithIdentifier:@"DetailViewController"];
Store *store = [annotationStoreDictionary objectForKey:view.annotation];
con.store = store;
[[self navigationController] pushViewController:con animated:YES];
}
问题在于 MKPointAnnotation 不符合 NSCoping 协议。它说:
Sending 'MKPointAnnotation *__strong' to parameter of incompatible type 'id<NSCoping>'
如何解决这个问题?
【问题讨论】:
-
正如@Jesper 建议的那样,子类化 MKPointAnnotation 并添加自定义属性以在注释对象本身中保留对每个注释的 Store 的引用。不需要单独的字典。另一种方法是:由于您已经有一个
Store类,只需使其实现MKAnnotation协议并将 Store 对象本身添加到映射中。这样,注释就是 Stores。要创建一个实现 MKAnnotation 的类,请参阅stackoverflow.com/questions/5939223/store-data-in-mkannotation 的一个示例。 -
+1,@安娜。实现
MKAnnotation是一个很好的选择。 -
@Anna,如果你提供这个作为答案,我会接受!感谢您的意见,它奏效了。
标签: objective-c mkannotation mkpointannotation