2010-07-15 1 views
1

여러 개의 주석이있는 mapView에서 managedObject를 mapDetailView로 전달하려고하는데, managedObject의 한 주석 만 전달됩니다. 이것은 tableView to tableDetaiView에서 잘 작동합니다. 어떤 도움을 주시면 감사하겠습니다. 이 경우에 UITableViewDataSource 방법을 구현하지 않는 때문에 내 코드 ...MapKit mapView에서 managedObject를 mapDetailView로 전달하는 방법

- (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]; 

} 

답변

2

indexPath는 선언되지 않은 것입니다! 당신이 여기서 성취하려는 것을 모르지만 한가지 이상의 해결책이있을 수 있다고 생각합니다. 먼저 주석에 object 속성을 추가하고이를 사용하여 주석에서 해당 NSManagedObject을 가져올 수 있습니다. 둘째, 을 사용하여 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 
} 

그것이 의해 복사되어 있기 때문에 여기 MapLocationNSCopying 프로토콜을 준수하셔야합니다 다음과 같이 사용할 것 setObject:forKey: 방법.

셋째, 당신은 MKAnnotation 프로토콜을 준수하여 NSManagedObject 서브 클래스에 모든 정보 (주소, 도시, 주 등)을 추가 할 수 있습니다, 당신은 객체에 해당하는 주석을 불러 오는 중 더 이상 문제가되지 않습니다 왜냐하면 그것은 동일 할 것이기 때문입니다.

+0

감사합니다 Eugenio - 그것은 나를 위해 일한 옵션 하나처럼 보입니다. 주석에 객체 속성을 추가했습니다. 컴파일러에게 여전히 새로운 속성이 프로토콜의 일부가 아니라는 경고를 받겠지 만? –

+0

이상한 점이 있습니다 ... 프로토콜 클래스에 대한 자세한 내용을 게시 할 수 있습니까? (인터페이스로 충분할 것입니다). 인터페이스 구현에서 @synthesize 속성을 확인하십시오. 고맙습니다. – eugeniodepalo