首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >设置自定义UIButton子类的操作

设置自定义UIButton子类的操作
EN

Stack Overflow用户
提问于 2011-05-05 22:29:59
回答 1查看 4K关注 0票数 3

几天来,我一直在尝试找到一种方法来调用一个自定义按钮上的action:selector,这个按钮派生自UIButton,我想将它与一个引脚注释关联起来。我使用子类的原因是,我想将一些数据传递给这个按钮对象,以便在按钮被按下时显示另一个显示这些数据的UIView

问题是,在执行时,当我单击pin注释时,它会打开它的按钮,而当我单击它时,什么也没有发生。在接收到这里设置的touchupInside事件时,不会调用我的showDetails:方法:

代码语言:javascript
运行
复制
[rightButton addTarget:self 
                action:@selector(showDetails:)
      forControlEvents:UIControlEventTouchUpInside];

我的问题是如何继承UIButton的方法,如addTarget:action:forControlEvents:buttonWithType:等,以便在我的子类中使用它们?

下面是关于这个问题的完整代码:

代码语言:javascript
运行
复制
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
{
    MKPinAnnotationView *annView=[[[MKPinAnnotationView alloc] 
        initWithAnnotation:annotation reuseIdentifier:@"MyPin"] autorelease];  
    annView.animatesDrop=FALSE;  
    annView.canShowCallout = YES;  
    [annView setSelected:YES]; 

    if ([[annotation title] isEqualToString:@"Current Location"]) {
        annView.pinColor = MKPinAnnotationColorRed;  
    }
    else {
        annView.pinColor = MKPinAnnotationColorPurple; 
    }

    annView.calloutOffset = CGPointMake(-5, 5);  

    PlaceMark *pm=(PlaceMark *)annotation;
    AddressItem *ai=[pm getData];

    //Another problem is that i cant set buttonWithType:UIButtonTypeDetailDisclosure cause it generate error cause it is a method of UIbutton superclass
    MyDetailButton* rightButton = [MyDetailButton buttonWithType:UIButtonTypeCustom];
    [rightButton setData:ai];

    [rightButton setTitle:annotation.title forState:UIControlStateNormal];

    [rightButton addTarget:self action:@selector(showDetails:) forControlEvents:UIControlEventTouchUpInside];

    annView.rightCalloutAccessoryView = rightButton;

    return annView;  
}

提前谢谢你。我希望我已经对我的问题给出了一个清楚的解释。

EN

回答 1

Stack Overflow用户

发布于 2011-05-05 22:42:35

我想你走错路了。要发送action @selector,你的类需要继承UIControl而不是UIButton。查看UIControl Class Reference我认为问题出在这里。如果我不明白你的问题,请不要犹豫地告诉我。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5899413

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档