2013-02-12 14 views
0

我想通過從JSON數組中拉取它來爲我的MKAnnotation添加一個字幕。我能夠獲得標題,並與下面的視圖控制器進行協調,但我無法弄清楚如何從JSON密鑰「cityName」中獲取字幕。任何幫助將是偉大的!謝謝!從JSON數組中拉取MKAnnotation字幕

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 

回答

1

這裏有什麼問題?只要做你正在做的與title財產。

+0

這一行 「 - (ID)initWithTitle:(的NSString *)TTL andCoordinate:(CLLocationCoordinate2D)C2D」 能」我只指定一個ttl和一個c2d變量? – Brandon 2013-02-12 08:24:10

+1

不,你可以添加你的'subtitle'屬性:' - (id)initWithTitle:(NSString *)ttl andCoordinate :(CLLocationCoordinate2D)c2d andSubtitle:(NSString *)subTitle'。請小心在'.h'中更改您的方法名稱 – Yaman 2013-02-12 08:25:33

+0

謝謝Yaman,這可能是新手,但我能否傳遞其他字符串?可以說我想要一個可以無形傳遞的自定義文件 – Brandon 2013-02-12 08:28:28

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"];