Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/110.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 从MKmapview上选择的MKAnnotation获取URI_Ios_Cocoa Touch_Core Data_Mapkit - Fatal编程技术网

Ios 从MKmapview上选择的MKAnnotation获取URI

Ios 从MKmapview上选择的MKAnnotation获取URI,ios,cocoa-touch,core-data,mapkit,Ios,Cocoa Touch,Core Data,Mapkit,这可能是一个设计错误而不是语法错误,所以如果我在这里编码的方向错误,请告诉我 我是Cocoa Touch/Objective-C新手,一直在学习有关核心数据和MapKit的教程。到目前为止,该应用程序在地图视图上为从核心数据获取的项目添加了注释。我创建了一个自定义注释对象(称为MapPin),它还包含一个URI(NSURL*),指向它所表示的核心数据中的对象。当用户选择注释时,我希望使用该注释的URI属性来查找该注释在coredata中表示的对象 如果我事先将注释添加到地图视图中,这将起作用。在

这可能是一个设计错误而不是语法错误,所以如果我在这里编码的方向错误,请告诉我

我是Cocoa Touch/Objective-C新手,一直在学习有关核心数据和MapKit的教程。到目前为止,该应用程序在地图视图上为从核心数据获取的项目添加了注释。我创建了一个自定义注释对象(称为MapPin),它还包含一个URI(NSURL*),指向它所表示的核心数据中的对象。当用户选择注释时,我希望使用该注释的URI属性来查找该注释在coredata中表示的对象

如果我事先将注释添加到地图视图中,这将起作用。在这里,我为每个对象“thing”添加了一个MapPin注释

稍后,在用户选择注释并单击annotationView标注中的按钮后,我想访问所选注释的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
我在调试器中注意到,在将注释添加到mapview之前,URI属性会显示正确的字符串。从映射视图中选择(MapPin)注释后,调试器中的URI属性仅显示“无效摘要”

当我尝试访问URI属性时,程序结束,我得到一个“线程1:程序接收信号:”EXC\U BAD\U 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;
}
-(MKAnnotationView*)地图视图:(MKMapView*)地图视图注释:(id)注释{
静态NSString*标识符=@“MapPin”;
if([annotation isKindOfClass:[MapPin class]]){
NSLog(@“注释是一个MapPin”);
TreeAnotationView*annotationView=(TreeAnotationView*)[\u mapView出列可重用AnnotationViewWithIdentifier:identifier];
如果(注释视图==nil){
annotationView=[[TreeAnnotationView alloc]initWithAnnotation:annotation重用标识符:标识符];
}否则{
annotationView.annotation=注释;
}
annotationView.enabled=是;
annotationView.canShowCallout=是;
//在详图索引中放置“详细信息”按钮
UIButton*detailButton=[UIButton Button类型:UIButtonTypedTailButton];
[detailButton setTitle:annotation.title for状态:UIControlStateNormal];
[detailButton addTarget:self action:@selector(showDetails:)for ControlEvents:UIControlEventTouchUpInside];
annotationView.rightCalloutAccessoryView=detailButton;
返回注释视图;
}
返回零;
}

为什么不将MapPin作为NSManagedObject的子类,并让它实现MKAnnotation

@interface MapPin : NSManagedObject <MKAnnotation>
@end
@接口映射pin:NSManagedObject
@结束
这样,您就可以创建一个且只有一个模型对象来表示管脚,并且该模型对象将通过CoreData持久化


但是您的“EXC\u BAD\u ACCESS”和“invalid summary”表明URI没有被保留(如果您使用的是ARC,则为strong)。

为什么不将您的MapPin作为NSManagedObject的子类,并让它实现MKAnnotation

@interface MapPin : NSManagedObject <MKAnnotation>
@end
@接口映射pin:NSManagedObject
@结束
这样,您就可以创建一个且只有一个模型对象来表示管脚,并且该模型对象将通过CoreData持久化


但是您的“EXC\u BAD\u ACCESS”和“invalid summary”表示URI没有被保留(如果您使用ARC,则为strong)。

所说的“不工作”是指编译器错误/警告还是崩溃?实际的错误/警告或崩溃消息/堆栈跟踪和MapPin.h会有所帮助。谢谢回复。不工作意味着程序崩溃。当我尝试访问MapPin类的uri属性时,我得到错误“Thread 1:Program Received signal:“EXC_BAD_access”。“日志中没有显示比这更有用的内容。”。我将添加“编辑我的帖子”以添加更多详细信息。看起来uri在MapPin中未正确声明或设置。您还可以显示MapPin.h和initWithName方法吗?您说的“不工作”是指编译器错误/警告还是崩溃?实际的错误/警告或崩溃消息/堆栈跟踪和MapPin.h会有所帮助。谢谢回复。不工作意味着程序崩溃。当我尝试访问MapPin类的uri属性时,我得到错误“Thread 1:Program Received signal:“EXC_BAD_access”。“日志中没有显示比这更有用的内容。”。我将添加“编辑我的帖子”以添加更多详细信息。看起来uri在MapPin中未正确声明或设置。还可以显示MapPin.h和initWithName方法吗?