Iphone MapKit如何将managedObject从mapView传递到mapDetailView

Iphone MapKit如何将managedObject从mapView传递到mapDetailView,iphone,xcode,annotations,mapkit,nsmanagedobjectcontext,Iphone,Xcode,Annotations,Mapkit,Nsmanagedobjectcontext,我正在尝试将managedObject从具有多个注释的mapView传递到仅传递managedObject的一个注释的mapDetailView。这在tableView到mapDetaiView中非常有效。任何帮助都将不胜感激。我的代码 - (void)viewDidLoad { [super viewDidLoad]; if (self.managedObjectContext == nil) { self.managedObjectContext = [(CrossroadsTr

我正在尝试将managedObject从具有多个注释的mapView传递到仅传递managedObject的一个注释的mapDetailView。这在tableView到mapDetaiView中非常有效。任何帮助都将不胜感激。我的代码

- (void)viewDidLoad {
[super viewDidLoad];

if (self.managedObjectContext == nil) { 
    self.managedObjectContext = [(CrossroadsTreasuresAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
}

// setup the mapView
[mapView removeAnnotations:mapView.annotations];
[mapView setMapType:MKMapTypeStandard];
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];
[self.view insertSubview:mapView atIndex:0];
[mapView setDelegate:self];

// setup the location coordnates to Victoria, TX
double lat = [@"28.825" doubleValue];
double lng = [@"-97.009" doubleValue];
CLLocationCoordinate2D rcoord;
rcoord.latitude = lat;
rcoord.longitude = lng;

// setup the map region
//MapLocation *annotation = [[MapLocation alloc] init];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(rcoord, 25000, 25000);
MKCoordinateRegion adjustRegion = [mapView regionThatFits:region];
[mapView setRegion:adjustRegion animated:YES];

NSError *error;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"GarageSaleItem" inManagedObjectContext:[self managedObjectContext]];
[fetchRequest setEntity:entity];
NSArray *markerObjects = [[self managedObjectContext] executeFetchRequest:fetchRequest error:&error];

for (int i = 0; i < [markerObjects count]; i++) {
    NSDictionary *marker = (NSDictionary *)[markerObjects objectAtIndex:i];

    // Set the annotation coordnates
    double lat = [[marker valueForKey:@"latitude"] doubleValue];
    double lng = [[marker valueForKey:@"longitude"] doubleValue];
    CLLocationCoordinate2D coord;
    coord.longitude = lng;
    coord.latitude = lat;

    // Create the annotation instatnce
    MapLocation *annotation = [[MapLocation alloc] init];

    // Set the annotation display information
    annotation.coordinate = coord;
    annotation.streetAddress = [marker valueForKey:@"streetAddress"];
    annotation.city = [marker valueForKey:@"city"];
    annotation.state = [marker valueForKey:@"state"];
    annotation.zipCode = [marker valueForKey:@"zipCode"];

    // Add the annotation the the map
    [self.mapView addAnnotation:annotation];
    [annotation release];
}
[fetchRequest release];
}


- (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:[MapLocation class]]) {
    // try to dequeue an existing pin view first
    static NSString* AnnotationIdentifier = @"com.coastalbendmedia.pin";
    MKPinAnnotationView* pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
    if (!pinView) {
        // if an existing pin view was not available, create one
        MKPinAnnotationView* customPinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease];

        customPinView.pinColor = MKPinAnnotationColorPurple;
        customPinView.animatesDrop = YES;
        customPinView.canShowCallout = YES;

        // add a detail disclosure button to the callout which will open a new view controller page
        //
        // note: you can assign a specific call out accessory view, or as MKMapViewDelegate you can implement:
        //  - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control;
        //
        UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        customPinView.rightCalloutAccessoryView = rightButton;

        return customPinView;
    } else {
        pinView.annotation = annotation;
    }
    return pinView;
}
return nil;
}


- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control; {


DetailViewController *controller = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];

// Pass the selected object to the new view controller.
[self.navigationController pushViewController:controller animated:YES];

// Set the detail item in the detail view controller.
    // THIS IS WHERE MY PROBLEM IS! indexPath Undeclared
NSManagedObject *selectedObject = [[self fetchedResultsController] objectAtIndexPath:indexPath];
controller.detailItem = selectedObject;
[controller release];

}
-(void)viewDidLoad{
[超级视图下载];
如果(self.managedObjectContext==nil){
self.managedObjectContext=[(CrossRoadStreaseAppDelegate*)[[UIApplication sharedApplication]委托]managedObjectContext];
}
//设置地图视图
[mapView removeAnnotations:mapView.annotations];
[mapView setMapType:MKMapTypeStandard];
[mapView SetZoomeEnabled:是];
[地图视图设置可克隆:是];
[self.view insertSubview:mapView索引:0];
[mapView setDelegate:self];
//设置与德克萨斯州维多利亚州的位置坐标
双lat=[@“28.825”双值];
双液化天然气=[@“-97.009”双值];
位置协调;
rcoord.纬度=纬度;
rcoord.经度=液化天然气;
//设置地图区域
//MapLocation*注释=[[MapLocation alloc]init];
mkcoordinaereregion=mkcoordinaereregionmakewithdistance(rcoord,25000,25000);
MKCoordinateRegion adjustRegion=[mapView RegionAtFits:region];
[地图视图设置区域:调整区域动画:是];
n错误*错误;
NSFetchRequest*fetchRequest=[[NSFetchRequest alloc]init];
NSEntityDescription*entity=[NSEntityDescription entityForName:@“GarageSaleItem”在托管对象上下文:[self-managedObjectContext]]中;
[FetchRequestSetEntity:entity];
NSArray*MarkerObject=[[self-managedObjectContext]executeFetchRequest:fetchRequest错误:&error];
对于(int i=0;i<[markerObjects count];i++){
NSDictionary*标记=(NSDictionary*)[MarkerObject对象对象索引:i];
//设置注释坐标
双纬度=[[marker valueForKey:@“纬度”]双纬度值];
双lng=[[marker valueForKey:@“经度”]双值];
CLLocationCoordinate2D coord;
坐标经度=液化天然气;
坐标纬度=纬度;
//创建注释instatnce
MapLocation*注释=[[MapLocation alloc]init];
//设置注释显示信息
annotation.coord=坐标;
annotation.streetAddress=[marker valueForKey:@“streetAddress”];
annotation.city=[marker valueForKey:@“city”];
annotation.state=[marker valueForKey:@“state”];
annotation.zipCode=[marker valueForKey:@“zipCode”];
//将注释添加到地图中
[self.mapView addAnnotation:annotation];
[注释发布];
}
[请求释放];
}
-(MKAnnotationView*)贴图视图:(MKMapView*)注释的贴图视图:(id)注释{
//如果是用户位置,只需返回nil。
if([annotation isKindOfClass:[MKUserLocation类]])
返回零;
//处理我们的两个自定义注释
//
if([annotation isKindOfClass:[MapLocation class]]){
//首先尝试将现有pin视图出列
静态NSString*注释标识符=@“com.coastalbendmedia.pin”;
MKPinAnnotationView*pinView=(MKPinAnnotationView*)[mapView出列可重用AnnotationViewWithIdentifier:AnnotationIdentifier];
如果(!pinView){
//如果现有接点视图不可用,请创建一个接点视图
MKPinAnnotationView*customPinView=[[MKPinAnnotationView alloc]initWithAnnotation:annotation重用标识符:AnnotationIdentifier]autorelease];
customPinView.pinColor=MKPinAnnotationColorPurple;
customPinView.animatesDrop=是;
customPinView.canShowCallout=是;
//将详细信息披露按钮添加到详图索引,这将打开一个新的视图控制器页面
//
//注意:您可以指定一个特定的调出附件视图,或者作为MKMAPVIEW委派,您可以实现:
//-(无效)地图视图:(MKMapView*)地图视图注释视图:(MKAnnotationView*)视图调用访问控制点击:(UIControl*)控制;
//
UIButton*rightButton=[UIButton Button类型:UIButtonTypedTailButton];
customPinView.rightCalloutAccessoryView=rightButton;
返回customPinView;
}否则{
pinView.annotation=注释;
}
返回pinView;
}
返回零;
}
-(无效)地图视图:(MKMapView*)地图视图注释视图:(MKAnnotationView*)视图调用访问控制点击:(UIControl*)控制;{
DetailViewController*控制器=[[DetailViewController alloc]initWithNibName:@“DetailViewController”捆绑包:nil];
//将选定对象传递给新的视图控制器。
[self.navigationController pushViewController:控制器已设置动画:是];
//在详图视图控制器中设置详图项目。
//这就是我的问题所在!未声明
NSManagedObject*selectedObject=[[self-fetchedResultsController]对象索引路径:indexPath];
controller.detailItem=selectedObject;
[控制器释放];
}

indepath
未声明,因为在本例中,您没有实现
UITableViewDataSource
方法!我不知道你想在这里实现什么,但我认为可能有不止一种可能的解决方案。首先,您可以尝试将
对象
属性添加到注释中,并使用该属性从注释中获取相应的
NSManagedObject
。其次,您可以使用
NSMutableDictionary
来存储对象,而不是
NSArray
。您将按如下方式使用它:

NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];

for(NSManagedObject *object in markerObjects) {
  // Create the annotation
  MapLocation *annotation = [[MapLocation alloc] init];

  [dictionary setObject:object forKey:annotation]
}
然后检索注释的相应对象,如下所示:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
  NSManagedObject *object = [dictionary objectForKey:view.annotation];

  // Do whatever you want with your object
}
请注意,此处的
MapLocation
必须符合
NSCopying
协议,因为它是由
setObject:forKey:
方法复制的


第三,您可以将所有信息(地址、城市、州等)添加到符合
MKAnnotation
协议的
NSManagedObject
子类中,这样您就不会再有检索与对象对应的注释的问题了,因为那是一样的。

谢谢尤金尼奥——看起来选项一对我有效。我添加了一个对象p