iphone - Mapkit addAnnotation异常: unrecognized selector sent to instance

标签 iphone ios ios6 mapkit mkannotation

我的与问题相关的代码:

注释.h文件

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>   

@interface CityAnnotation : NSObject <MKAnnotation> {
    NSString *name;
    NSString *country;
    CLLocationCoordinate2D coords;
}

@property (nonatomic,copy) NSString *name;
@property (nonatomic,copy) NSString  *country;
@property (nonatomic,assign) CLLocationCoordinate2D coords;

-(id)initWithTitle:(NSString *)cityname AndCountry:(NSString *)countryname AndCoords: (CLLocationCoordinate2D)coordinate;


@end

注释.m文件

#import "CityAnnotation.h"

@implementation CityAnnotation

@synthesize name,country,coords;

-(id)initWithTitle:(NSString *)cityname AndCountry:(NSString *)countryname AndCoords:(CLLocationCoordinate2D)coordinate
{
    self  = [super init];
    if (self){
    self.name = cityname;
    self.country = countryname;
    self.coords = coordinate;
    }
    return self;

}

@end

我导入我的 map View :

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import "tableViewController.h"
#import "City.h"
#import "CityAnnotation.h"

在 View Controller 中添加注解。这是在 [self.mapView addAnnotation:newAnnotation];

上发生错误的地方
- (void)viewDidLoad
{
    [super viewDidLoad];
    CityAnnotation *newAnnotation = [[CityAnnotation alloc]initWithTitle:self.SelectedCity.name AndCountry:self.SelectedCity.country AndCoords:self.SelectedCity.location];
    [self.mapView addAnnotation:newAnnotation];


}

我收到这个错误:

-[CityAnnotation coordinate]: unrecognized selector sent to instance 0x85c7d70
2013-06-17 12:15:57.694 JsonTest[1769:c07] *** Terminating app due to uncaught exception     'NSInvalidArgumentException', reason: '-[CityAnnotation coordinate]: unrecognized selector     sent to instance 0x85c7d70'

最佳答案

您的 CityAnnotation 类必须提供名为 coordinate 的属性/方法。查看MKAnnotation Protocol Reference了解详情。

您只需将 coords 属性重命名为 coordinate 即可解决您的问题。

关于iphone - Mapkit addAnnotation异常: unrecognized selector sent to instance,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17154440/

相关文章:

ios - 使 UICollectionViewCell 出现在 UICollectionView 的中心

iphone - CATransform3DRotate 旋转 360 度

ios - WKWebView 页面进入透明状态栏

iphone - 部分页面 curl 动画

iphone - UIScrollView:单击将其滚动到顶部

ios - Apple 将停止接受使用 UIWebView API 的应用程序提交

ios - 使用导航 Controller 在 View Controller 之间进行切换

当从链接安装应用程序时,iOS 应用程序传递特定的 ID

ios - 无法使用 Facebook 共享链接内容向网址添加描述和标题

ios6 - iOS Autolayout 按比例调整 UIViews 的大小?