【问题标题】:MapKit How to pass managedObject from mapView to mapDetailViewMapKit 如何将 managedObject 从 mapView 传递到 mapDetailView
【发布时间】:2010-07-15 15:02:43
【问题描述】:

我试图将一个 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];

}

【问题讨论】:

    标签: iphone xcode annotations mapkit nsmanagedobjectcontext


    【解决方案1】:

    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:方法复制而来的。

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

    【讨论】:

    • 谢谢 Eugenio - 看起来选项一对我有用。我在注释中添加了一个对象属性。尽管我仍然收到编译器警告,但新属性不是协议的一部分?
    • 这很奇怪......你能发布更多关于你的协议类的细节吗? (界面就足够了)。检查您是否在接口实现中 @synthesize 属性。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2013-05-26
    • 2012-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多