嗨,我有一个带有注释对象的 NSMutableArray,以下代码不会添加注释:
在 .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> 的注释对象数组?我没有收到任何错误,也不会显示任何注释。