我看到其他一些人也有这个问题。 我正在尝试关注如何在MapView上创建动画引脚的在线教程。发送到实例的无法识别的选择器?
我已经实现的代码,如图教程和除了我收到此异常的项目构建罚款:
-[MKPointAnnotation iconN]: unrecognized selector sent to instance
我有“MKPinAnnotationView”的一个子类,并在.m文件我创建这个方法:
- (void)setAnnotation:(id<MKAnnotation>)annotation {
[super setAnnotation:annotation];
//Place *place = [[Place alloc] init];
Place *place = (Place *)annotation;
//The following line is where the program sends "SIGABRT"
icon = [UIImage imageNamed:[NSString stringWithFormat:@"pin_%d.png", [place.iconN intValue]]];
[iconView setImage:icon];
}
以下是我的“模型”中称为Place.h/.m的几个部分。 这里是我创建'iconN'属性的地方。
@property (retain, nonatomic) NSNumber *iconN;
在这里,我合成它:
@synthesize iconN = _iconN;
任何帮助是极大的赞赏。
编辑: 这里是Place.h和Place.m
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@interface Place : NSObject <MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *title;
}
@property (retain, nonatomic) NSNumber *iconN;
@property (nonatomic, copy) NSString *title;
@property (nonatomic) CLLocationCoordinate2D coordinate;
- (id)initWithLong:(CGFloat)lon Lat:(CGFloat)lat iconNumber:(NSNumber *)iconNumber;
@end
而且Place.m
#import "Place.h"
@implementation Place
@synthesize coordinate;
@synthesize iconN = _iconN;
@synthesize title;
- (id)initWithLong:(CGFloat)lon Lat:(CGFloat)lat iconNumber:(NSNumber *)iconNumber {
self = [super init];
if (self) {
coordinate = CLLocationCoordinate2DMake(lat, lon);
self.iconN = iconNumber;
}
return self;
}
- (NSString *)title {
return [NSString stringWithFormat:@"Bus: %d", [self.iconN intValue]];
}
- (NSString *)subtitle {
return [NSString stringWithFormat:@"bus[%d] from database.", [self.iconN intValue] - 1];
}
@end
我对MapKit并不熟悉,但是对于我来说这件事非常突出:' - [MKPointAnnotation iconN]:发送给实例的无法识别的选择器是该类是'MKPointAnnotation'。因此,您收到的注释实际上不是“Place”对象,而是“MKPointAnnotation”对象 - 您不能只投射到“Place”。我怀疑问题的根源在于你首先创建注释对象的地方。 – pmdj 2012-02-23 22:19:43
Place是否实现了MKAnnotation协议? “注释”是一种地方吗?换句话说,'[annotation isKindOfClass:[Place class]]'是否返回true? – mamills 2012-02-23 22:21:05
@pmjordan 你钉了它! 我回去看看我的项目。我使用方法'addAnnotations:'添加了3个其他注释,出于某种原因,即使它们不是“Place”对象,我在添加它们时也调用了“setAnnotation”方法。也许我应该首先检查传递的对象是否为'Place'对象,如果不是,则什么都不做? – 2012-02-26 07:47:50