Я пытаюсь передать 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];
}
indexPath
не объявлен, потому что в этом случае вы не реализуете метод 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:
.
В-третьих, вы можете добавить всю информацию (адрес, город, штат и т. Д.) В свой подкласс NSManagedObject
, который соответствует протоколу MKAnnotation
, и у вас не будет больше проблема с получением аннотации, соответствующей объекту, поскольку это будет то же самое.