2013-05-29 27 views
1

我有一個應用程序,繪製在地圖視圖上的引腳。如何通過地圖上的公開按鈕調用segue?

每個引腳使用此代碼來顯示詳細披露按鈕,當點擊時調用一個showDetail方法,然後調用prepareForSegue方法。我認爲這裏有很多額外的工作。

我應該消除showDetail並只調用prepareForSegue方法嗎?但我將如何通過MyLocation對象?

下面是代碼:

Thx提前!

+0

你'showDetailView'嘗試使用'segue.destinationViewController',但直到'prepareForSegue'才能做到這一點。但是你可能根本不需要'showDetailView',而應該使用標準的'calloutAccessoryControlTapped'委託方法,如下所述。 – Rob

回答

3

通常你無法添加目標按鈕,而是剛剛成立的rightCalloutAccessoryView(像你這樣),然後寫一個calloutAccessoryControlTapped方法,如:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { 
    static NSString *identifier = @"MyLocation"; 
    if ([annotation isKindOfClass:[MyLocation class]]) { 

     MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier:identifier]; 
     if (annotationView == nil) { 
      annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier]; 
      annotationView.enabled = YES; 
      annotationView.canShowCallout = YES; 
      annotationView.image = [UIImage imageNamed:@"locale.png"]; // since you're setting the image, you could use MKAnnotationView instead of MKPinAnnotationView 
      annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
     } else { 
      annotationView.annotation = annotation; 
     } 

     return annotationView; 
    } 

    return nil; 
} 

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control 
{ 
    if (![view.annotation isKindOfClass:[MyLocation class]]) 
     return; 

    // use the annotation view as the sender 

    [self performSegueWithIdentifier:@"DetailVC" sender:view]; 
} 

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(MKAnnotationView *)sender 
{ 
    if ([segue.identifier isEqualToString:@"DetailVC"]) 
    { 
     DetailViewController *destinationViewController = segue.destinationViewController; 

     // grab the annotation from the sender 

     destinationViewController.receivedLocation = sender.annotation; 
    } else { 
     NSLog(@"PFS:something else"); 
    } 
} 
+0

請注意,我簡化了一下'viewForAnnotation'。不僅擺脫了「目標」的東西,而且也不需要引用你的類伊娃,因爲'mapView'是方法的一個參數。 – Rob