简体   繁体   English

在UIButton的选择器上传递参数

[英]Pass parameter on selector of UIButton

I have a detailDisclousure button on the callout of a MKAnnotation . 我有一个detailDisclousure上的标注按钮MKAnnotation When this button is pressed I need to call a method passing a parameter that identifies the annotation that called it. 当按下此按钮时,我需要调用一个传递参数的方法,该参数标识调用它的annotation How could it be done? 怎么做?

this is my viewForAnnotation: 这是我的viewForAnnotation:

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

if ([annotation isKindOfClass:[Annotation class]])
{
    static NSString* identifier = @"identifier";

    MKPinAnnotationView* pinView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];

    if (pinView == nil) 
    {
        pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier]autorelease];
    }

    Annotation *antt = annotation;

    pinView.canShowCallout = YES;
    pinView.draggable =    YES;

    UIButton* detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

    [detailButton performSelector:@selector(goToViewWithAnnotation:) withObject:antt];

    pinView.rightCalloutAccessoryView = detailButton;

    return pinView;
}

else 
{
    return nil; 
}
}

And this is the method that should be called with the parameter: 这是应该使用参数调用的方法:

-(void)goToViewWithAnnotation:(Annotation *)annotation
{
    NextViewController *nextView = [[NextViewController alloc]initWithNibName:@"NextViewController" bundle:nil];

nextView.id = annotation.id;

[self.navigationController nextView animated:YES];
}

您只能通过UIButtontag属性传递任何NSInteger

It's not possible to send data with the -addTarget:action:forControlEvent: for UIButton : 不能使用-addTarget:action:forControlEvent:UIButton发送数据:

[detailButton addTarget:self action:@selector(goToViewWithAnnotation:) forControlEvents:UIControlEventTouchUpInside];

Try to handle the data with some other way, when you trigger the selector. 触发选择器时,尝试以其他方式处理数据。 Like saving a pointer or var as you like. 就像保存指针或var一样。

UIButton have these callers as you can see: UIButton具有这些调用方,如您所见:

- (void)action
- (void)action:(id)sender
- (void)action:(id)sender forEvent:(UIEvent *)event

I think you can add a property(which is (Annotation *)annotation) in .h file. 我认为您可以在.h文件中添加一个属性(即(Annotation *)annotation)。 Then you can use annotation in the "-(void)goToViewWithAnnotation" method. 然后,您可以在“-(void)goToViewWithAnnotation”方法中使用注释。 Or you can customized a new button which inherits UIControl 或者您可以自定义一个继承UIControl的新按钮

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM