自定义UIButton子类的设置操作

问题描述:

我一直在尝试几天,以找到如何调用操作的方法:自定义按钮上的选择器,从UIButton派生,我想将其与注释关联。我使用子类的原因是我想将一些数据传递给此按钮对象,以便在按下按钮时显示另一个UIView显示这些数据。自定义UIButton子类的设置操作

问题是,当执行时,当我点击pin注释时,它会打开它的按钮,当我点击它时,什么都不会发生。我showDetails:方法不叫上接受touchupInside事件作为我这里设置:

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

预先感谢您。我希望我已经对我的问题给出了明确的解释。

+0

不要继承UIButton - 请参阅[this SO question](http://stackoverflow.com/questions/5045672/create-uibutton-subclass)。 – Rayfleck 2011-05-05 14:48:19

+0

谢谢你的答复,但它与链接中的问题有点不同,因此我建议我。因为我需要将数据传递到按钮,这就是为什么我使用子类。我需要做的是将人物数据从一个特定的针注释传递到一个新的视图来显示它们。如此类推:当点击包含人物数据的针注释(它们在地图上是多个)时,应该显示另一个视图消除这些人的数据。这就是为什么我想添加一个个性化的按钮到MKPinAnnotationView对象,但我有这个问题。可以存在另一种解决方案而不是它? thx – Deni 2011-05-05 15:51:47

+0

如果您继承UIButton,以便您可以将用户信息嵌入到每个按钮中,则不需要。相反,有2个你应该实现的委托方法:mapView:annotationView:calloutAccessoryControlTapped:和mapView:didSelectAnnotationView:这两个都会告诉你这个视图。因此,参考view.annotation来知道哪个基础数据项与该引脚相关联,并且从注释中您应该能够确定是谁。 – Rayfleck 2011-05-05 17:47:50

我想你错了。发送动作@selector你的类需要继承UIControl而不是UIButton。退房UIControl Class Reference我认为问题在这里。 不要犹豫,告诉我,如果我不明白你的问题。

+0

非常感谢回复。事实上,我从UIButton继承到UIControl,但问题仍然存在,因为如果我们不使用MyDetailButton * rightButton = [MyDetailButton buttonWithType:UIButtonTypeDetailDisclosureDisclosure];按钮不能显示在MKPinAnnotationView对象(annView)中;或类似的东西,因为这不是UICOntrol类的一部分,或者我错了?所以,我试图找到一些关于MKPinAnnotationView对象点击的技巧,它将打开一个显示数据的新视图。 但据我所知,它没有行动方法,再次感谢 – Deni 2011-05-05 15:31:44

+0

我不认为这是这里的关键问题。如果我没有记错,UIButton从UIControl继承,所以从UIButton继承应该也会给你所有来自UIControl的操作。 – Nicholas1024 2011-05-05 15:47:54

+0

@ Nicholas1024我认为是从UIButton继承,但在我的应用程序中测试。它会在调用buttonWithType时引发异常:UIButtonTypeDetailDisclosure – Deni 2011-05-05 15:59:18