我正在尝试将具有多个注释的mapView中的managedObject传递给mapDetailView,其中只传递了一个managedObject的注释。这在mapView to 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];
}
答案 0 :(得分:2)
indexPath
未声明,因为在这种情况下您没有实施UITableViewDataSource
方法!我不知道你在这里想要完成什么,但我认为可能有多种可能的解决方案。首先,您可以尝试向注释添加object
属性,并使用该属性从注释中获取相应的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:
方法复制的。
第三,您可以将所有信息(地址,城市,州等)添加到符合NSManagedObject
协议的MKAnnotation
子类中,您将不再具有以下问题:检索对应于对象的注释,因为它将是相同的。