【问题标题】:Not able to initialize CLLocationCoordinate2D iVar in MKAnnotation Classe无法在 MKAnnotation 类中初始化 CLLocationCoordinate2D iVar
【发布时间】:2011-10-21 20:55:22
【问题描述】:

我正在尝试将 MKAnnotations 放在 MKMapView 上。我正在遵循各种可用教程中建议的程序。我已经从 NSobject 类创建了 PlaceMark.h/.m 文件,如下所示。 PlaceMark.h

#import <Foundation/Foundation.h>
#import <MapKit/MKAnnotation.h>
@interface PlaceMark : NSObject<MKAnnotation>
{
CLLocationCoordinate2D coordinate;
NSString *title;
}
@property (nonatomic,copy) NSString *title;
@property (nonatomic,readonly) CLLocationCoordinate2D coordinate;
@end

PlaceMark.m

#import "PlaceMark.h"

@implementation PlaceMark
@synthesize coordinate,title;

-(void)dealloc
{
   [title release];
   [super dealloc];
}

@end

在我持有 MKMapView 的 viewController 中,在 viewDidload 中我有以下代码

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

MKCoordinateRegion region;
region.center.latitude=37.3305262;
region.center.longitude=-122.0290935;
region.span.latitudeDelta=0.01;
region.span.longitudeDelta=0.01;

[parkingMap setRegion:region animated:YES];

PlaceMark *ann=[[[PlaceMark alloc]init]autorelease]; // I have also tired it using explicitly defining method -(id)initwithTitle.... but it did not worked.

ann.title=@"Test";
ann.coordinate= region.center;
[parkingMap addAnnotation:ann];
}

谁能告诉我我做错了什么?顺便说一句,我正在使用 Xcode4.2 和 iOS sdk5.0 和 不使用情节提要/自动引用计数

谢谢 苏米特

【问题讨论】:

    标签: ios mkmapview mkannotation xcode4.2 mkpinannotationview


    【解决方案1】:

    您已将coordinate 属性定义为readonly,因此您无法设置它。

    将其更改为readwrite

    另外,如果您只需要一个带有titlesubtitle 和可设置coordinate 的基本注释对象,您可以使用内置的MKPointAnnotation 类而不是定义自己的类。创建和初始化将是相同的(只需将您的类名替换为MKPointAnnotation)。

    【讨论】:

    • 它显示了同样的错误。之后,我删除了整个内容并重新开始,这次成功了。
    猜你喜欢
    • 1970-01-01
    • 2021-07-01
    • 2017-02-23
    • 2020-01-11
    • 1970-01-01
    • 2015-09-27
    • 2021-11-03
    • 2012-08-01
    • 2012-10-10
    相关资源
    最近更新 更多