您好我有一個NSMutableArray裏與註釋對象的follwoing代碼將不添加註釋:IOS:addAnnotations到MapView的工作不
在.H我
@property (retain, nonatomic) MarketsDataController *marketList;
在.M我已經(添加註釋不起作用什麼也不顯示地圖)
_marketList = [_marketService.marketsDataController retain];
NSLog(@"%@!!!", [_marketList objectInListAtIndex:0].title);
[mapView addAnnotation:[_marketList objectInListAtIndex:0]];
[mapView addAnnotation:[_marketList objectInListAtIndex:1]];
[mapView addAnnotation:[_marketList objectInListAtIndex:2]];
MarketsListDataController看起來是這樣的:
#import "MarketsDataController.h"
//The following interface is used for private methods
@interface MarketsDataController()
- (void)initializeMarketsList;
@end
@implementation MarketsDataController
//Set initial values for instance variables
- (id)init {
NSLog(@"init MarketsDataController");
if (self = [super init]) {
[self initializeMarketsList];
return self;
}
return nil;
}
- (void)initializeMarketsList{
NSMutableArray *marketsList = [[NSMutableArray alloc] init]; //Initialize the product list
_marketsList = marketsList; //Set the markets list to the markets list
}
//Return the number of products
- (NSUInteger)countOfList {
return [_marketsList count];
}
//Return a product within the list
- (MapAnnotation *)objectInListAtIndex:(NSUInteger)theIndex {
return [_marketsList objectAtIndex:theIndex];
}
- (void)addAnnotationToList:(MapAnnotation *)mapAnnotation{
NSLog(@"Adding market");
[_marketsList addObject:mapAnnotation];
}
- (void)dealloc {
NSLog(@"DEALLOC MarketsDataController");
[_marketsList release];
[super dealloc];
}
@end
MapAnnotation .M樣子:
#import "MapAnnotation.h"
@implementation MapAnnotation
@synthesize coordinate, title, subtitle;
- (id)init{
CLLocationCoordinate2D location;
location.latitude = 0;
location.longitude = 0;
return [self initWithCoordinate:coordinate title:nil subtitle:nil];
}
- (id)initWithCoordinate:(CLLocationCoordinate2D) c title:(NSString *)t subtitle:(NSString *)st{
self = [super init];
coordinate = c;
title = [t retain];
subtitle = [st retain];
return self;
}
- (void) dealloc{
[title release];
[subtitle release];
[super dealloc];
}
@end
我創建它們並將它們添加另一個類,如:
if (![latitude isEqual:[NSNull null]] && ![longitude isEqual:[NSNull null]]) {
NSLog(@"%d", i);
NSLog(@"%@", title);
CLLocationCoordinate2D coordinate;
coordinate.longitude = [latitude doubleValue];
coordinate.longitude = [longitude doubleValue];
[self buildMarketsList:coordinate title:title subtitle:@""]; //build the browse list product
}
方法如下:
- (void)buildMarketsList:(CLLocationCoordinate2D)c title:(NSString *)t subtitle:(NSString *)st{
MapAnnotation *mapAnnotation = [[MapAnnotation alloc]initWithCoordinate:c title:t subtitle:st];
[_marketsDataController addAnnotationToList:mapAnnotation];
[mapAnnotation release];
}
哪有我添加一個註釋對象數組,實現< MKAnnotation>?我沒有收到任何錯誤,也沒有註釋顯示。
_marketsList是類型的NSMutableArray的?這將如何工作_marketList.marketsList?還有_前綴的變量被建議只在setter中使用。 – Anupdas 2013-04-20 06:16:27
爲什麼你保留_marketList.marketsList? – Balu 2013-04-20 06:16:35
_marketsList是一個擁有保存註釋對象的NSMutable數組的對象。 – wwjdm 2013-04-20 06:46:48