2013-06-12 89 views
1

在我的應用程序出現問題後,我爲MKMapView編寫了測試應用程序。我的問題是我不能在地圖上放置多個針腳。在viewDidLoad:方法使用此代碼,10個引腳應該從座標指定顯示,在對角:MKAnnotation - 未出現多個註釋

Annotation *annotation = [[Annotation alloc] init]; 
NSMutableArray *array = [[NSMutableArray alloc] init]; 
for (int i = 0; i < 10; i++) { 
    annotation.title = [NSString stringWithFormat:@"Pin%i", i]; 
    annotation.subtitle = [NSString stringWithFormat:@"%i", i]; 
    annotation.coordinate = CLLocationCoordinate2DMake(37.41181 + (i * .1), -122.11809 + (i * .1)); 
    [array addObject:annotation]; 
} 
[map addAnnotations:array]; 

Annotation是單純只是:

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

@interface Annotation : NSObject <MKAnnotation> 

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

-(id)init; 

@end 


@implementation Annotation 
@synthesize title, subtitle, coordinate; 

-(id)init { 
    self = [super init]; 
    return self; 
} 

@end 

而不是看到10個引腳的,我只看到我的'當前位置'和一個遙遠的引腳,其標題和副標題爲「Pin9」和「9」。它似乎只添加數組中最近的元素。

有人可以看到發生了什麼? 謝謝。

回答

0

擺弄一點點後,我發現,如果你把線工程:

Annotation *annotation = [[Annotation alloc] init]; 

for循環,不在外面。噢,生活和學習。

+0

那麼,這是在For循環之外,你只創建一個Annotation並在每個循環中重新分配它。放置在循環中時,您將爲每個循環創建一個新的Annotation並添加它。 – Padin215

1

註釋必須在for循環內分配。

Annotation * annotation = [[Annotation alloc] init];

+1

您的意思是: 'Annotation * annotation = [[Annotation alloc] init];' – Jeeter

+0

對不起,這是一個錯字錯誤 – Varun