如何获得UIButton的父标签?

时间:2014-10-29 15:15:53

标签: ios uibutton

我有一个简单的应用程序,附有地图和别针。点击地图上的图钉后,会创建pinView,因为我有一个名为ButtonPressed的动作按钮。 我想知道它被按下了哪个引脚。我试过像:

      (IBAction)ButtonPressed:(id)sender
{

UIButton *but = (UIButton*) sender;

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

}

但无论我为5设置标记,它总是返回0。 好的,我发布了你想要的添加按钮的代码:

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

1 个答案:

答案 0 :(得分: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];

}