MapKit如何将管理对象从mapView传递到mapDetailView

发布于 2024-09-10 00:09:02 字数 4667 浏览 5 评论 0原文

我正在尝试将一个托管对象从具有多个注释的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幸福不弃 2024-09-17 00:09:02

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 子类中,这样您就不再拥有检索与对象相对应的注释的问题,因为那是相同的。

indexPath is undeclared because you're not implementing a UITableViewDataSource 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 an object property to your annotation, and use that to get the corresponding NSManagedObject from the annotation. Second, you could use an NSMutableDictionary to store the objects instead of an NSArray. You would use it as follows:

NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];

for(NSManagedObject *object in markerObjects) {
  // Create the annotation
  MapLocation *annotation = [[MapLocation alloc] init];

  [dictionary setObject:object forKey:annotation]
}

Then you would retrieve the corresponding object for an annotation like this:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
  NSManagedObject *object = [dictionary objectForKey:view.annotation];

  // Do whatever you want with your object
}

Please note that MapLocation here must conform to the NSCopying protocol since it is copied by the setObject:forKey: method.

Third, you could add all the informations (address, city, state, etc.) to your NSManagedObject subclass that conforms to the MKAnnotation protocol, and you won't have anymore the problem of retreiving an annotation corresponding to an object, since that would be the same.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文