简体   繁体   中英

iOS MapKit didAddAnnotationViews method not called

I'm trying to load annotations from a separate class once data has been parsed. The data is parsing correctly which I follow through the NSLog. I am sending the data to the class responsible for loading the MKMapView. This class receives the correct data.

Here is the code for the class that parses my data (ParseData.m):

AnnotationsTest *loadFriend = [[AnnotationsTest alloc] init];
    [loadFriend loadOutAnnotations:friendFound];
    [loadFriend loadOutAnnotationsAgain];
    [loadFriend release];

The method being called in my second class (MapViewController.m):

-(void)loadOutAnnotations:(NSMutableArray *)friendData

with the following code:

    CLLocationCoordinate2D workingCoordinate;

workingCoordinate.latitude = [[friendData objectAtIndex:5] floatValue];
workingCoordinate.longitude = [[friendData objectAtIndex:4] floatValue];
MapAnnotation *appleStore1 = [[MapAnnotation alloc] initWithCoordinate:workingCoordinate];
[appleStore1 setTitle:@"Title"];
[appleStore1 setSubtitle:@"subtitle"];
[appleStore1 setUserData:[friendData objectAtIndex:6]];
[appleStore1 setAnnotationType:iCodeBlogAnnotationTypeApple];

[self.mapView addAnnotation:appleStore1];

And finally, here is my code to respond to the addAnotation events (MapViewController.m):

- (void)mapView:(MKMapView *)mapViews didAddAnnotationViews:(NSArray *)views {
id myAnnotation = [mapViews.annotations objectAtIndex:0];
[mapViews selectAnnotation:myAnnotation animated:YES];
NSLog(@"didAddAnnotationViews is called");}

The annotation will load perfectly if I hard code the variables, don't pass the NSMutableArray through the method, and call upon the method in the MapViewController class viewDidLoad section. When I try to call upon the method to add annotations from another class, the didAddAnnotationViews method does not respond.

Please advise. Thanks in advance!!

Evan

So I looked at your code and it's expected behavior: A map annotation is just the abstract representation of an annotation. You do have to "manually" add it to the map. The map THEN asks the delegate for the VIEW it should use to DISPLAY the annotation. So it's no wonder it's not showing up when you don't add it to the map :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM