Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

custom UIButton, unrecognized selector sent to instance

i am trying to subclass a UIButton to attach a property to it, but as you might already guess i get an " unrecognized selector sent to instance" error.

Exact error : [UIButton setAnnotPin:]: unrecognized selector sent to instance

Here is are the important parts of the code:

MapViewController.h:

  #import "UIRightPinAccessoryButton.h"

MapViewController.m:

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{ 
 //some other code..

UIRightPinAccessoryButton *rightAccessoryButton = [UIRightPinAccessoryButton buttonWithType:UIButtonTypeDetailDisclosure];
rightAccessoryButton.frame = CGRectMake(0, 0, 35, 35);
rightAccessoryButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
rightAccessoryButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[rightAccessoryButton addTarget:self action:@selector(rightAccessoryButtonTapped:) forControlEvents:UIControlEventTouchUpInside]; 

rightAccessoryButton.annotPin = annot;  **// Error comes here**
}

and here is my UIRightPinAccessoryButton class:

UIRightPinAccessoryButton.h

#import "Annotations.h"

@interface UIRightPinAccessoryButton : UIButton{
  Annotations *annotPin;
} 

@property (nonatomic) Annotations *annotPin;

UIRightPinAccessoryButton.m

#import "UIRightPinAccessoryButton.h"

@implementation UIRightPinAccessoryButton

@synthesize annotPin;

@end

I really don´t understand why xcode is looking for the setAnnotPin: method at UIButton instead of my custom UIRightPinAccessoryButton??

like image 928
Kito Avatar asked Aug 27 '26 17:08

Kito


1 Answers

This section [UIRightPinAccessoryButton buttonWithType:UIButtonTypeDetailDisclosure];

Will not return an object of type UIRightPinAccessoryButton but it will return an object of type UIButton So change it to

UIRightPinAccessoryButton *rightAccessoryButton = [[UIRightPinAccessoryButton alloc] init];

Also it seems there is no way of changing the type of button to UIButtonTypeDetailDisclosure once its subclassed, you may set your own image to make it look the same though
Also you cant subclass the buttonWithType easily, please read more from iPhone: Override UIButton buttonWithType to return subclass

like image 81
Omar Abdelhafith Avatar answered Aug 29 '26 08:08

Omar Abdelhafith



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!