3

I have been trying for a couple of days to find a way how to call action:selector on a custom button, derived from UIButton, which I want to associate with a pin annotation. The reason that I used a subclass is that I want to pass some data into this button object, in order to show another UIView displaying these data when the button is pressed.

The problem is that while executing, when I click on the pin annotation, it opens its button, and when I click on it, nothing happens. My showDetails: method is not called on receiving the touchupInside event as I set here:

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

My question is how to inherit the methods of UIButton, like addTarget:action:forControlEvents:, buttonWithType:, etc., in order to use them in my subclass?

Here is the complete code concerning the problem:

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

Thank you in advance. I hope that I have given a clear explanation of my problem.

jscs
  • 63,694
  • 13
  • 151
  • 195
Deni
  • 31
  • 1
  • 2
  • Don't subclass UIButton - see [this SO question](http://stackoverflow.com/questions/5045672/create-uibutton-subclass). – Rayfleck May 05 '11 at 14:48
  • thank u for your reply.but it's a little bit different from the problem in the link u suggest me.Cause i need to pass data into the button,that's why i use subclass. what I need to do, is to pass person data from a specific pin annotation to a new view for displaying them.smth like that: when clicking in a pin annotation containing person data(they are multiple in map),this should display another view diplaying these person data. That's why i thought to add a personalized button to the MKPinAnnotationView object but i have problem with this. Can exist another solution instead of it? thx – Deni May 05 '11 at 15:51
  • If you're subclassing UIButton so you can embed the user info in each button, you don't need to. Instead, there are 2 delegate methods you should implement: mapView:annotationView:calloutAccessoryControlTapped: and mapView:didSelectAnnotationView: Both of these will tell you the view. So reference view.annotation to know which underlying data item is associated with that pin, and from the annotation you should be able to figure out who that is. – Rayfleck May 05 '11 at 17:47
  • UIButton is a concrete class, not a class cluster. I've subclassed UIButton without any problems. Apple quite explicitly tells you what classes can and can not be subclassed. – Feloneous Cat Dec 12 '12 at 19:40

1 Answers1

0

I think you're on wrong way. To send action @selector your class need inherit from UIControl and not UIButton. Check out UIControl Class Reference I think the problem is here. Do not hesitate to tell me if I did not understand your question.

TheRonin
  • 1,305
  • 1
  • 12
  • 18
  • thanks a lot for the reply. In fact i substituted the inherit from UIButton to UIControl, but the problem still persist, because the button can't be not displayed in MKPinAnnotationView object (annView) if we dont use MyDetailButton* rightButton = [MyDetailButton buttonWithType:UIButtonTypeDetailDisclosure]; or something like that, because that is nnot part of UICOntrol class, or i'm wrong? So, i'm trying to find some trick that on MKPinAnnotationView object clicking, it will be opened a new view with data displayed. but as i know, it doesnt have an action method , thanks again – Deni May 05 '11 at 15:31
  • I don't think that's the key problem here. If I remember correctly, UIButton inherits from UIControl, and so inheriting from UIButton should give you all the actions from UIControl as well. – Nicholas1024 May 05 '11 at 15:47
  • @Nicholas1024 i thought so by inherit from UIButton, but while testing in my appl. it throws exception for the case of calling buttonWithType:UIButtonTypeDetailDisclosure – Deni May 05 '11 at 15:59
  • Hm. Try calling it like myDetailButton* rightbutton = [UIButton buttonWithType:] – Nicholas1024 May 05 '11 at 16:13