MKMapView addAnnotation不在MapView上

时间:2012-02-16 10:05:31

标签: objective-c ios5 mkmapview mapkit

我需要一些帮助,因为我无法在mapView中显示注释。 我正在尝试使用此函数更新注释:

- (void)addAnnotationsInMap:(NSMutableDictionary *)dico
{
    NSLog(@"Agences Dictionary dico4324 : %@", dico);  
    if ([dico count] > 0)
    {
        NSLog(@"Count : %d", [dico count]);
        for(int i = 0; i < [dico count]; i++)
        {
            CGFloat latDelta = [[[dico objectForKey:[NSString stringWithFormat:@"%d", i]] objectForKey:@"latitude"] floatValue];
            CGFloat longDelta   = [[[dico objectForKey:[NSString stringWithFormat:@"%d", i]] objectForKey:@"longitude"] floatValue];      
            CLLocationCoordinate2D newCoord =
            {
                (latDelta),
                (longDelta)
            };      
            MapPointAnnotations *mp = [[MapPointAnnotations alloc]
                                       initWithCoordinate:newCoord
                                                    title:[[dico objectForKey:[NSString stringWithFormat:@"%d", i]] objectForKey:@"name"]
                                                 subTitle:[[dico objectForKey:[NSString stringWithFormat:@"%d", i]] objectForKey:@"street"]];  
            [agenceMapView addAnnotation:mp];    
        }
    }
}

如果我使用[self addAnnotationsInMap:Dico]在同一个类(LocalizeViewController)中调用我的函数,则注释位于mapView(agenceMapView)上。

我需要从另一个类(RightSideBarViewController)调用我的函数,但是如果我调用RightSideBarViewController类,则注释不会显示在mapView上。

我调用函数addAnnotationsInMap(Class RightSideBarViewController):

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *identifier = @"LocaliserTop";
    UIStoryboard *storyboard;
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
    {
        storyboard = [UIStoryboard storyboardWithName:@"iPhone" bundle:nil];
    } else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        storyboard = [UIStoryboard storyboardWithName:@"iPad" bundle:nil];
    }
    LocalizeViewController *localizeViewController = (LocalizeViewController *)[storyboard instantiateViewControllerWithIdentifier:identifier];    
    [self.slidingViewController anchorTopViewOffScreenTo:ECLeft animations:nil onComplete:^{
        CGRect frame = self.slidingViewController.topViewController.view.frame;
        self.slidingViewController.topViewController = localizeViewController;
        self.slidingViewController.topViewController.view.frame = frame;
        [self.slidingViewController resetTopView];
    }];
    [localizeViewController addAnnotationsInMap:agencesDictionary];
}

你有什么想法吗?

1 个答案:

答案 0 :(得分:1)

你不应该打电话

[localizeViewController addAnnotationsInMap:agencesDictionary];

之后

LocalizeViewController *localizeViewController = (LocalizeViewController *)[storyboard instantiateViewControllerWithIdentifier:identifier];

因为agenceMapView仍然是nil。所以你基本上在做

[nil addAnnotation:mp];

要解决此问题,您应该等到localizeViewController从nib文件加载(这需要一些时间)。

例如,你可以把

localizeViewController.annotations = agencesDictionary;

而不是

[localizeViewController addAnnotationsInMap:agencesDictionary];

然后在viewDidLoad

[self addAnnotationsInMap:annotations];