2012-06-07 80 views
5

要創建一個故事板的iOS項目地圖標註,我用:添加披露按鈕MKPointAnnotation

CLLocationCoordinate2D annotationCoord3; 

     annotationCoord3.latitude = 34.233129; 
     annotationCoord3.longitude = -118.998644; 

     MKPointAnnotation *annotationPoint3 = [[MKPointAnnotation alloc] init]; 
     annotationPoint3.coordinate = annotationCoord3; 
     annotationPoint3.title = @"Another Spot"; 
     annotationPoint3.subtitle = @"More than a Fluke"; 
     [_mapView addAnnotation:annotationPoint3]; 

它的偉大工程,但我想補充披露按鈕,這樣我可以把塞克到一個新的查看控制器和顯示圖像。這可能嗎?

Thx提前,

--bd--

+0

添加東西的註解視圖不註釋;) –

回答

9

聲明你的類是MKMapViewDelegate。然後,添加

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { 


    MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"String"]; 
    if(!annotationView) { 
     annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"String"]; 
     annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
    } 

    annotationView.enabled = YES; 
    annotationView.canShowCallout = YES; 

    return annotationView; 
} 

然後添加:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control { 
    // Go to edit view 
    ViewController *detailViewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; 
    [self.navigationController pushViewController:detailViewController animated:YES]; 

} 

...視圖控制器可以是任何你定義(我用筆尖文件...)

+0

只有當你添加'[MapView的setDelegate:個體經營]工程;'的'viewDidLoad中某處( );'或左右:) – doonot

+0

是的。在oredr使用mapview委託方法,你需要首先符合mkmapviewdelegate在.h和設置viewcontroller作爲mapview的代表...希望我是有幫助的 –

2

阿克塞爾的答案是正確的(我只是upvoted):您必須實現MKMapViewDelegate並將其實例分配給MKMapView的Delegate屬性。對於使用的MonoTouch的,這裏的端口:

class MapDelegate : MKMapViewDelegate 
{ 
    public override MKAnnotationView GetViewForAnnotation (MKMapView mapView, NSObject annotation) 
    { 
     MKAnnotationView annotationView = mapView.DequeueReusableAnnotation ("String"); 
     if (annotationView == null) { 
      annotationView = new MKAnnotationView(annotation, "String"); 
      annotationView.RightCalloutAccessoryView = new UIButton(UIButtonType.DetailDisclosure); 
     } 
     annotationView.Enabled = true; 
     annotationView.CanShowCallout = true; 
     return annotationView; 
    } 

    public override void CalloutAccessoryControlTapped (MKMapView mapView, MKAnnotationView view, UIControl control) 
    { 
     // Push new controller to root navigation controller. 
    } 
} 
相關問題