几天来,我一直在尝试找到一种方法来调用一个自定义按钮上的action:selector,这个按钮派生自UIButton,我想将它与一个引脚注释关联起来。我使用子类的原因是,我想将一些数据传递给这个按钮对象,以便在按钮被按下时显示另一个显示这些数据的UIView。
问题是,在执行时,当我单击pin注释时,它会打开它的按钮,而当我单击它时,什么也没有发生。在接收到这里设置的touchupInside事件时,不会调用我的showDetails:方法:
[rightButton addTarget:self
action:@selector(showDetails:)
forControlEvents:UIControlEventTouchUpInside];我的问题是如何继承UIButton的方法,如addTarget:action:forControlEvents:、buttonWithType:等,以便在我的子类中使用它们?
下面是关于这个问题的完整代码:
- (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;
}提前谢谢你。我希望我已经对我的问题给出了一个清楚的解释。
发布于 2011-05-05 22:42:35
我想你走错路了。要发送action @selector,你的类需要继承UIControl而不是UIButton。查看UIControl Class Reference我认为问题出在这里。如果我不明白你的问题,请不要犹豫地告诉我。
https://stackoverflow.com/questions/5899413
复制相似问题