1

我正在尝试制作一个自定义 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];
    }

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

4

3 回答 3

2

似乎您正在尝试设置您的只读属性。

您将其声明为只读:

@property (nonatomic, readonly) CLLocationCoordinate2D  coordinate;

但随后您尝试使用 setter:

shop.coordinate = coords;

readonly意味着不会为其他类定义设置器来利用。

编辑: 我认为你应该在你的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];
    }
于 2013-03-28T13:17:14.923 回答
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

于 2013-03-28T13:23:27.507 回答
0

您已将该属性设置为只读,它只为该属性设置了一个 getter 方法。因此,请保留该属性并再次尝试。

于 2013-03-28T13:19:15.773 回答