2014-10-29 88 views
0

我有簡單的應用程序與地圖和附加到它的引腳。在地圖上點擊地圖後,pinView已創建,在此我有一個名爲「ButtonPressed」的按鈕。 我想知道它是從哪個針被按下。我想是這樣的:如何獲取UIButton的父標籤?

 (IBAction)ButtonPressed:(id)sender 
{ 

UIButton *but = (UIButton*) sender; 

NSLog(@"SEG PLZ SEG CMON %tu",but.superview.tag); 
[self performSegueWithIdentifier:@"segu" sender:sender]; 

} 

但它總是返回0,不管我設置的標籤爲5 好吧,我張貼的代碼添加按鈕,你想:

- (MKPinAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)myannotation{ 

    MKPinAnnotationView *view = nil; 
    //MKPinAnnotationView *view=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"HotSpotsLoc"]; 

    if(myannotation !=mapView.userLocation){ 
     view = (MKPinAnnotationView *) 
     [mapView dequeueReusableAnnotationViewWithIdentifier:@"identifier"]; 
     if(nil == view) { 
      view = [[MKPinAnnotationView alloc] 
        initWithAnnotation:myannotation reuseIdentifier:@"identifier"]; 
     } 

        UIButton *btnViewVenue = [UIButton buttonWithType:UIButtonTypeContactAdd]; 
     [view.rightCalloutAccessoryView setTag:50]; 
     [btnViewVenue addTarget:self action:@selector(ButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; 

     view.rightCalloutAccessoryView=btnViewVenue; 
     view.enabled = YES; 
     view.pinColor = MKPinAnnotationColorPurple; 
     view.canShowCallout = YES; 
     view.multipleTouchEnabled = NO; 
     //view.animatesDrop = YES; 

     UIButton *btnViewwVenue = [UIButton buttonWithType:UIButtonTypeContactAdd ] ; 

     [btnViewwVenue addTarget:self action:@selector(addImage:) forControlEvents:UIControlEventTouchUpInside]; 
     [view setTag:50]; 

     view.leftCalloutAccessoryView=btnViewwVenue; 
     view.enabled = YES; 
     view.pinColor = MKPinAnnotationColorPurple; 
     view.canShowCallout = YES; 
     view.multipleTouchEnabled = NO; 
    } 
    return view; 
} 
+0

你能後,你添加的按鈕在視圖請 – 2014-10-29 15:19:59

+0

當使用rightCalloutAccessoryView和leftCalloutAccessoryView,你不需要做'addTarget'也不需要使用標籤(請不要代碼)也不需要看超級視圖(請不要)。相反,實現'calloutAccessoryControlTapped'委託方法,註釋對象可以通過view.annotation獲得。請參閱http://stackoverflow.com/questions/14805954/mkannotationview-push-to-view-controller-when-detaildesclosure-button-is-clicked。 – Anna 2014-10-29 16:19:17

回答

0

設置leftCalloutAccessoryViewrightCalloutAccessoryView不一定會將視圖添加爲view的子視圖。如果你需要明確這種關係,可以考慮子類UIButton

@interface MyButton : UIButton 
@property (weak) MKPinAnnotationView *annotation; 
@end 

@implementation MyButton 
@end 


// Then... 

(IBAction)ButtonPressed:(id)sender 
{ 

    MyButton *but = (MyButton*) sender; 

    NSLog(@"SEG PLZ SEG CMON %tu",but.annotation.tag); 
    [self performSegueWithIdentifier:@"segu" sender:sender]; 

}