无法在ios中创建自定义注释类

时间:2013-03-28 13:14:40

标签: iphone ios objective-c mkmapview mkannotation

我正在尝试创建一个自定义的MKAnnotation类,以便它可以保存其他信息,但我不断收到此错误:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-    [AnnotationForId setCoordinate:]: unrecognized selector sent to instance 0x16f63340'

我已经尝试过如何创建一个并且我已经按照我在网上找到的内容而且我很困惑为什么我会一直收到这个错误。

这是我的自定义注释类。

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

@interface AnnotationForId : NSObject <MKAnnotation >{
    NSString *title;
    NSString *subtitle;
    CLLocationCoordinate2D coordinate;
    NSInteger shopId;
}
@property(nonatomic)NSInteger shopId;
@property (nonatomic,  copy) NSString * title;
@property (nonatomic, copy) NSString * subtitle;
@property (nonatomic, readonly) CLLocationCoordinate2D  coordinate;
@end

我在.m类中合成了属性变量。

我尝试在主控制器中调用自定义类:

for(Shop *shops in feed.shops){
        NSLog(@"reached");
        AnnotationForId *shop = [[AnnotationForId alloc] init];
        CLLocationCoordinate2D coords = CLLocationCoordinate2DMake(shops.latitude, shops.longitude);
        shop.coordinate = coords;
        //[[CLLocationCoordinate2DMake(shops.latitude, shops.longtitude)]];
        shop.title = shops.name;
        shop.subtitle = @"Coffee Shop";
        [map addAnnotation:shop];
    }

为什么这不起作用的任何帮助都会很棒。 感谢。

3 个答案:

答案 0 :(得分:2)

好像你正试图设置你的只读属性。

您将其声明为只读:

@property (nonatomic, readonly) CLLocationCoordinate2D  coordinate;

但是你要尝试使用setter:

shop.coordinate = coords;

readonly 意味着不会为其他类定义任何setter。

修改 我认为您应该为AnnotationForId类添加便利初始化程序,如:

-(id)initWithCoordinate:(CLLocationCoordinate2D)coord
{ 
    self = [super init]; // assuming init is the designated initialiser of the super class
    if (self)
    { 
        coordinate = coord;
    }
    return self;  
}

所以你的代码看起来像这样:

for(Shop *shops in feed.shops){
        NSLog(@"reached");
        CLLocationCoordinate2D coords = CLLocationCoordinate2DMake(shops.latitude, shops.longitude);
        AnnotationForId *shop = [[AnnotationForId alloc] initWithCoordinate:coords];
        //[[CLLocationCoordinate2DMake(shops.latitude, shops.longtitude)]];
        shop.title = shops.name;
        shop.subtitle = @"Coffee Shop";
        [map addAnnotation:shop];
    }

答案 1 :(得分:1)

这是关于如何创建自定义AnnotationView的简单示例。

创建自定义AnnotationView

#import <MapKit/MapKit.h>

@interface AnnotationView : MKPlacemark

@property (nonatomic, readwrite, assign) CLLocationCoordinate2D coordinate;

@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) NSString *subtitle;

// you can put here any controllers that you want. (such like UIImage, UIView,...etc)

@end

并在.m file

#import "AnnotationView.h"

@implementation AnnotationView

- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate addressDictionary:(NSDictionary *)addressDictionary
{
    if ((self = [super initWithCoordinate:coordinate addressDictionary:addressDictionary]))
    {
        self.coordinate = coordinate;
    }
    return self;
}

@end

//使用注释在相关的#import "AnnotationView.h"

中添加.m file
CLLocationCoordinate2D pCoordinate ;
pCoordinate.latitude = LatValue;
pCoordinate.longitude = LanValue;

// Create Obj Of  AnnotationView class  

AnnotationView *annotation = [[AnnotationView alloc] initWithCoordinate:pCoordinate addressDictionary:nil] ;

    annotation.title = @"I m Here";
    annotation.subtitle = @"This is Sub Tiitle";

[self.mapView addAnnotation:annotation];

以上是简单的示例如何创建AnnotationView

答案 2 :(得分:0)

您已将该属性指定为readonly,它仅为该属性设置getter方法。所以请保留该物业并再次尝试。