从 MKmapview 上选择的 MKAnnotation 获取 URI
这可能是一个设计错误而不是语法错误,所以如果我在这里编码的方向错误,请告诉我。
我是 Cocoa Touch/Objective-C 的新手,我一直在学习 Core Data 和 MapKit 的教程。到目前为止,该应用程序在地图视图上为从核心数据获取的项目添加了注释。我创建了一个自定义注释对象(称为 MapPin),它还包含一个 URI (NSURL*),该 URI 指向它所代表的 Core Data 中的对象。当用户选择一个注释时,我想使用该注释的 URI 属性来查找该注释代表 coredata 中的哪个对象。
如果我事先将注释添加到地图视图中,这将起作用。这里我为每个对象“事物”添加一个 MapPin 注释
//viewWillAppear function in my ViewController
NSURL *uri = [[thing objectID] URIRepresentation];
MapPin *annotation = [[[MapPin alloc] initWithName:thing.common description:thing.latin coordinate:coordinate uri:uri] autorelease];
NSLog(@"MapPin URI: %@", [annotation.uri absoluteString]); //This works!
[_mapView addAnnotation:annotation];
NSLog(@"Placed Map Pin: %@", thing.common);
稍后,在用户选择注释并单击注释视图标注中的按钮后,我想访问所选注释的 URI,
//the UIButton click action in my view controller
MSPTreesAppDelegate *del = (MSPTreesAppDelegate *)[UIApplication sharedApplication].delegate;
NSArray *annArray = _mapView.selectedAnnotations;
MapPin *selectedPin = [annArray objectAtIndex:0];
NSLog(@"Selected Pin Name: %@", selectedPin.name); //Works fine
NSLog(@"URI PASSED: %@", [selectedPin.uri absoluteString]); //Doesn't work
NSURL* uriForTree = selectedPin.uri; //also doesn't work
我在添加注释之前在调试器中注意到对于地图视图,URI 属性显示正确的字符串。从地图视图中选择 (MapPin) 注释后,调试器中的 URI 属性仅显示“无效摘要”。
当我尝试访问 URI 属性时,程序结束,并收到“线程 1:程序收到信号:“EXC_BAD_ACCESS”。错误。除此之外,日志没有显示任何有用的内容。
我假设 MKAnnotationView 或 MKMapView 不支持我的 MKAnnotation 对象的自定义 URI 属性,但我无法弄清楚 URI 在哪里丢失。有没有办法从所选注释中检索我的 URI 属性?欢迎提出实现相同概念的其他方法建议。
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
static NSString *identifier = @"MapPin";
if ([annotation isKindOfClass:[MapPin class]]) {
NSLog(@"Annotation is a MapPin");
TreeAnnotationView *annotationView = (TreeAnnotationView *) [_mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil) {
annotationView = [[TreeAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
} else {
annotationView.annotation = annotation;
}
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
//Place details button in callout
UIButton * detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[detailButton setTitle:annotation.title forState:UIControlStateNormal];
[detailButton addTarget:self action:@selector(showDetails:) forControlEvents:UIControlEventTouchUpInside];
annotationView.rightCalloutAccessoryView = detailButton;
return annotationView;
}
return nil;
}
This may be a design error instead of a syntax error, so let me know if I'm coding in the wrong direction here.
I'm new to Cocoa Touch/Objective-C and I've been working through tutorials on Core Data and MapKit. So far the app places an annotation on a mapview for items fetched from Core Data. I made a custom annotation object (called MapPin) that also holds a URI (NSURL*) pointing to the object in Core Data that it represents. When the user selects an annotation I want to use the URI property of that annotation to find which object in coredata the annotation represents.
This works if I add the annotation to the mapview beforehand. Here I add a MapPin annotation for each object "thing"
//viewWillAppear function in my ViewController
NSURL *uri = [[thing objectID] URIRepresentation];
MapPin *annotation = [[[MapPin alloc] initWithName:thing.common description:thing.latin coordinate:coordinate uri:uri] autorelease];
NSLog(@"MapPin URI: %@", [annotation.uri absoluteString]); //This works!
[_mapView addAnnotation:annotation];
NSLog(@"Placed Map Pin: %@", thing.common);
Later, after the user selects an annotation and clicks a button in the annotationView callout, I want to access the URI for the selected annotation
//the UIButton click action in my view controller
MSPTreesAppDelegate *del = (MSPTreesAppDelegate *)[UIApplication sharedApplication].delegate;
NSArray *annArray = _mapView.selectedAnnotations;
MapPin *selectedPin = [annArray objectAtIndex:0];
NSLog(@"Selected Pin Name: %@", selectedPin.name); //Works fine
NSLog(@"URI PASSED: %@", [selectedPin.uri absoluteString]); //Doesn't work
NSURL* uriForTree = selectedPin.uri; //also doesn't work
I notice in the debugger that before I add the annotation to the mapview, the URI property shows the correct string. After the (MapPin) annotation is selected from the map view, the URI property in the debugger just shows "invalid summary".
When I try to access the URI property the program ends and I get a "Thread 1: Program Received signal: "EXC_BAD_ACCESS"." error. The Log doesn't show anything helpful besides that.
I assume that my custom URI property for my MKAnnotation object isn't supported by the MKAnnotationView or the MKMapView, but I can't figure out where the URI is getting lost. Is there a way I can retrieve my URI property from the selected annotation? Other suggestions for methods to accomplish the same concept are welcome.
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
static NSString *identifier = @"MapPin";
if ([annotation isKindOfClass:[MapPin class]]) {
NSLog(@"Annotation is a MapPin");
TreeAnnotationView *annotationView = (TreeAnnotationView *) [_mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil) {
annotationView = [[TreeAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
} else {
annotationView.annotation = annotation;
}
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
//Place details button in callout
UIButton * detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[detailButton setTitle:annotation.title forState:UIControlStateNormal];
[detailButton addTarget:self action:@selector(showDetails:) forControlEvents:UIControlEventTouchUpInside];
annotationView.rightCalloutAccessoryView = detailButton;
return annotationView;
}
return nil;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么不让您的 MapPin 成为 NSManagedObject 的子类并让它实现 MKAnnotation?
这样,您就可以用一个且唯一的一个模型对象来表示引脚,并且该模型对象通过 CoreData 进行持久化。
但是您的“EXC_BAD_ACCESS”和“无效摘要”表明该 URI 未被保留(如果您使用的是 ARC,则该值很强)。
Why not make your MapPin be a subclass of NSManagedObject and have it implement MKAnnotation?
That way you one and only one model object to represent the pin and that model object is persisted through CoreData.
But your "EXC_BAD_ACCESS" and "invalid summary" indicate that the URI isn't' being retained (strong if you're using ARC).