我正在尝试将UIButton子类化为附加属性,但正如您可能已经猜到的那样,我发现“无法识别的选择器发送到实例”错误。
确切错误:[UIButton setAnnotPin:]:发送到实例的无法识别的选择器
以下是代码的重要部分:
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?
答案 0 :(得分:3)
本节[UIRightPinAccessoryButton buttonWithType:UIButtonTypeDetailDisclosure];
不会返回UIRightPinAccessoryButton
类型的对象,但会返回UIButton类型的对象
所以把它改成
UIRightPinAccessoryButton *rightAccessoryButton = [[UIRightPinAccessoryButton alloc] init];
此外,似乎没有办法将按钮的类型更改为UIButtonTypeDetailDisclosure一旦其子类,您可以设置自己的图像,使其看起来相同但 你也不能轻易地将buttonWithType子类化,请从iPhone: Override UIButton buttonWithType to return subclass
中阅读更多内容