我正在尝试子类化 UIButton 以将属性附加到它,但您可能已经猜到我收到“无法识别的选择器发送到实例”错误。
确切错误:[UIButton setAnnotPin:]: unrecognized selector sent to instance
以下是代码的重要部分:
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**
}
这是我的 UIRightPinAccessoryButton 类:
UIRightPinAccessoryButton.h
#import "Annotations.h"
@interface UIRightPinAccessoryButton : UIButton{
Annotations *annotPin;
}
@property (nonatomic) Annotations *annotPin;
UIRightPinAccessoryButton.m
#import "UIRightPinAccessoryButton.h"
@implementation UIRightPinAccessoryButton
@synthesize annotPin;
@end
我真的不明白为什么 xcode 在 UIButton 上寻找 setAnnotPin: 方法而不是我的自定义 UIRightPinAccessoryButton?