Xcode MapKit,并在MKLocalSearch后添加新PIN

Xcode MapKit,并在MKLocalSearch后添加新PIN,xcode,ios7,mapkit,mkannotation,mklocalsearch,Xcode,Ios7,Mapkit,Mkannotation,Mklocalsearch,我正在运行一个MKLocalSearch请求,当我找到poi时,我想根据我找到的poi的坐标放置一个pin。NSLog@annotations%d,[mapView.annotations计数];即使POI的坐标有效,也返回0 - (void)resultsForButtonTapped:(NSString *)search { arrayOfPOIs = [[NSMutableArray alloc] init]; arrayOfAnnotaions = [[NSMutableArray a

我正在运行一个MKLocalSearch请求,当我找到poi时,我想根据我找到的poi的坐标放置一个pin。NSLog@annotations%d,[mapView.annotations计数];即使POI的坐标有效,也返回0

- (void)resultsForButtonTapped:(NSString *)search
{

arrayOfPOIs = [[NSMutableArray alloc] init];
arrayOfAnnotaions = [[NSMutableArray alloc] init];
NSLog(@"%@",search);
MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];
MKCoordinateRegion region;
request.naturalLanguageQuery = search;
request.region = region;

[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
localsearch = [[MKLocalSearch alloc] initWithRequest:request];

[localsearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) {
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

    if (error != nil) { 

        return;
    }else{
        if ([response.mapItems count] == 0) {
            [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"No Results",nil)
                                    message:nil
                                   delegate:nil
                          cancelButtonTitle:NSLocalizedString(@"OK",nil) otherButtonTitles:nil] show];
            return;
        }
        for (MKMapItem *item in response.mapItems)
        {
            NSString *name = item.name;
            NSLog(@"%@",name);
            [arrayOfPOIs addObject:name];
        }

        results = response;
        NSLog(@"%d",[arrayOfPOIs count]);

        for (int i=0; i<[results.mapItems count]; i++) {
            MKMapItem* itemPOI = results.mapItems[i];
            NSLog(@"Result: %@",itemPOI.placemark.name);


            NSLog(@"Result: %@",itemPOI.placemark.name);

            MKPlacemark* annotation= [[MKPlacemark alloc] initWithPlacemark:itemPOI.placemark];

            MKPointAnnotation *marker = [MKPointAnnotation new];
            marker.coordinate = annotation.coordinate;

            [mapView addAnnotation:marker];
            NSLog(@"annotations %d",[mapView.annotations count]);
        }

        [self loadTheDataAgain];
    }
}];
  //    NSLog(@"outside the block, the count is %lu",(unsigned long)[arrayOfPOIs count]);

    NSLog(@"stuff %@",arrayOfAnnotaions);

}

在同一个NSLog中,确保mapView不是nil。如。NSLog@annotations%d,mapView=%@[mapView.annotations count],mapView;在使用它之后,mapView在此特定函数中为空。我正在viewDidLoad方法中实例化mapView,并在uitableview单元格中对其进行子类化。在索引路径方法行的单元格内,mapView不是Null。resultsForButtonTapped方法是否与表视图在同一视图控制器中?从何时何地调用resultsForButtonTapped?发布cellForRowAtIndexPath方法可能会有所帮助。是的,它位于同一视图控制器中。mapView是我的视图控制器btw的一个属性