0

JSON配列から字幕を取得して、MKAnnotationに字幕を追加しようとしています。タイトルを取得し、以下のようにView Controllerと調整することはできますが、JSONキー「cityName」から字幕を取得するために何をすべきかわかりません。どんな助けでも素晴らしいでしょう!ありがとうございました!

MapViewController.m

location.latitude = [dictionary[@"placeLatitude"] doubleValue];
location.longitude = [dictionary[@"placeLongitude"] doubleValue];  

newAnnotation = [[MapViewAnnotation alloc] initWithTitle:dictionary[@"placeName"]
                                               andCoordinate:location];

MapViewAnnotation.h

@interface MapViewAnnotation : NSObject <MKAnnotation> {
NSString *title;
CLLocationCoordinate2D coordinate; 
}

@property (nonatomic, copy) NSString *title;
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *subtitle;
- (id)initWithTitle:(NSString *)ttl andCoordinate:(CLLocationCoordinate2D)c2d;
@end

MapViewAnnotation.m

#import "MapViewAnnotation.h"
@implementation MapViewAnnotation
@synthesize title, coordinate, subtitle;

- (id)initWithTitle:(NSString *)ttl andCoordinate:(CLLocationCoordinate2D)c2d {
title = ttl;
coordinate = c2d;
subtitle = [SUBTITLE PULLED FROM JSON]
return self;
}
@end
4

2 に答える 2

1

注釈が持つすべてのプロパティを初期化しないという状況が発生する可能性があるため、それらすべてを init メソッドに入れないことをお勧めします。

これを行うだけで、MapViewAnnotation.m または h を変更する必要はありません。

location.latitude = [dictionary[@"placeLatitude"] doubleValue];
location.longitude = [dictionary[@"placeLongitude"] doubleValue];  

newAnnotation = [[MapViewAnnotation alloc] initWithTitle:dictionary[@"placeName"]
                                           andCoordinate:location];
newAnnotation.subtitle = dictionary[@"cityName"];
于 2013-02-12T20:16:13.300 に答える
1

ここで何が問題ですか?titleプロパティで行っていることを正確に実行してください。

于 2013-02-12T08:14:06.670 に答える