MapKit如何将管理对象从mapView传递到mapDetailView
我正在尝试将一个托管对象从具有多个注释的mapView 传递到仅传递一个托管对象注释的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];
}
I am trying to pass a managedObject from a mapView with multiple annotation to a mapDetailView with only one annotation of the managedObject passed. This works great in a tableView to mapDetaiView. Any help would be appreciated. My code ...
- (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];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
indexPath
未声明,因为在这种情况下您没有实现UITableViewDataSource
方法!我不知道你想在这里完成什么,但我认为可能有不止一种可能的解决方案。首先,您可以尝试向注释添加一个object
属性,并使用它从注释中获取相应的NSManagedObject
。其次,您可以使用 NSMutableDictionary 而不是 NSArray 来存储对象。您可以按如下方式使用它:然后您将检索注释的相应对象,如下所示:
请注意,此处的
MapLocation
必须符合NSCopying
协议,因为它是由setObject:forKey:
方法。第三,您可以将所有信息(地址、城市、州等)添加到符合
MKAnnotation
协议的NSManagedObject
子类中,这样您就不再拥有检索与对象相对应的注释的问题,因为那是相同的。indexPath
is undeclared because you're not implementing aUITableViewDataSource
method in this case! I don't know what you're trying to accomplish here, but I think that there could be more than one possible solution. First, you could try to add anobject
property to your annotation, and use that to get the correspondingNSManagedObject
from the annotation. Second, you could use anNSMutableDictionary
to store the objects instead of anNSArray
. You would use it as follows:Then you would retrieve the corresponding object for an annotation like this:
Please note that
MapLocation
here must conform to theNSCopying
protocol since it is copied by thesetObject:forKey:
method.Third, you could add all the informations (address, city, state, etc.) to your
NSManagedObject
subclass that conforms to theMKAnnotation
protocol, and you won't have anymore the problem of retreiving an annotation corresponding to an object, since that would be the same.