【问题标题】:Pass parameter on selector of UIButton在 UIButton 的选择器上传递参数
【发布时间】:2012-07-23 13:52:16
【问题描述】:

MKAnnotation 的标注上有一个 detailDisclousure 按钮。当按下这个按钮时,我需要调用一个方法,传递一个参数,该参数标识调用它的annotation。怎么可能?

这是我对注解的看法:

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

这是应该使用参数调用的方法:

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

nextView.id = annotation.id;

[self.navigationController nextView animated:YES];
}

【问题讨论】:

    标签: ios parameters uibutton selector


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      无法使用-addTarget:action:forControlEvent:UIButton 发送数据:

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

      当您触发选择器时,请尝试以其他方式处理数据。就像根据需要保存指针或变量一样。

      UIButton 有这些调用者,如你所见:

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

      【讨论】:

      【解决方案3】:

      我认为您可以在 .h 文件中添加一个属性(即 (Annotation *)annotation)。然后你可以在“-(void)goToViewWithAnnotation”方法中使用注解。或者您可以自定义一个继承 UIControl 的新按钮

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-08-01
        • 1970-01-01
        • 2012-08-31
        • 1970-01-01
        • 2017-12-30
        • 1970-01-01
        相关资源
        最近更新 更多