Reputation: 1385
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??
Upvotes: 3
Views: 1471
Reputation: 21221
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
Upvotes: 3