2014-10-16 27 views
0

在我的地圖上,我有一個rightCalloutAccessoryViewUIButtonTypeDetailDisclosure。我想檢測用戶是否點擊註釋的中心或UIButtonTypeDetailDisclosure。 例如,如果他點擊中心:執行名爲XYZ的segue,如果在UIButtonTypeDetailDisclosure上點擊:執行名爲ABC的segued。檢測用戶是否點擊註釋的中心或右邊的UIButtonTypeDetailDisclosure?

這是我的代碼。問題是UIButtonTypeDetailDisclosure和註釋執行名爲「infoDetail」的同一個segue。我用rightCalloutAccessoryViewleftCalloutAccessoryView上的標籤嘗試不同的配置,以檢測哪一個被點擊,但是如何通過右側的UIButtonTypeDetailDisclosure來檢測註釋的中心?

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views { 
    MKAnnotationView *aView; 
    for (aView in views) { 
     if (![aView.annotation isKindOfClass:[MKUserLocation class]]) { 
      MKAnnotationView* annotationView = aView; 

      annotationView.rightCalloutAccessoryView = [UIButton buttonWithType: UIButtonTypeDetailDisclosure]; 
      annotationView.rightCalloutAccessoryView.tag = 1; 

      annotationView.leftCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeContactAdd]; 
      annotationView.leftCalloutAccessoryView.tag = 2; 
     } 
    } } 


- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control { 
    if ([control tag] == 1) { 
     // Right Accessory Button Tapped 
     [self performSegueWithIdentifier:@"infoDetail" sender:self]; 

    } else if ([control tag] == 2) { 
     // Left Accessory Button Tapped 
     [self performSegueWithIdentifier:@"contactDetail" sender:self]; 
    } } 

回答

0

我在mapView上用觸摸手勢做了一個自定義按鈕。我不認爲這是處理你的項目的乾淨方式,但它對我來說很有效。我用一個自定義註釋視圖SMCalloutView做到了這一點。 https://github.com/nfarina/calloutview

UIButton *leftDisclosure = [UIButton buttonWithType:UIButtonTypeCustom]; 
leftDisclosure.backgroundColor = [UIColor clearColor]; 
topDisclosure.frame = theframeYouWant; 

[topDisclosure addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(disclosureTapped:)]]; 

annotationView.rightAccessoryView = topDisclosure; 

這很棘手,我希望它能幫助你。

相關問題