1

我正在尝试将一个 managedObject 从一个带有多个注释的 mapView 传递到一个 mapDetailView,只传递一个 managedObject 的注释。这在 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];

}
4

1 回答 1

2

indexPath未声明,因为在这种情况下您没有实现UITableViewDataSource方法!我不知道你想在这里完成什么,但我认为可能有不止一种可能的解决方案。首先,您可以尝试向object注释添加一个属性,并使用它从注释中获取相应的属性NSManagedObject。其次,您可以使用 anNSMutableDictionary来存储对象,而不是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:方法复制的。

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

于 2010-07-23T08:08:49.800 回答