在方法之间传递数组中的数据

Passing data in an array between methods

我正在视图中构建一个已加载的数组,然后想在 viewForAnnotation 方法中访问其中包含的一些数据,以在地图视图标注中显示图像。我已经在实现下面而不是在我的方法中声明了它,但总是发现尽管它在 ViewDidLoad 中被正确加载和处理,但它在 viewForAnnotation 方法中始终为空。我在其他程序中使用了类似的变量设置,但不明白为什么它在这里不起作用,抱歉我对此很陌生,它可能很简单我错过了:-/

我的问题只是如何在 viewForAnnotation 方法中访问 viewDidLoad 中加载到 jsonTeach 数组中的数据?

这是我在这两种方法中的代码,除了我的问题,其他一切都工作正常。

int annotationIndex = 0;
NSMutableArray * jsonTeach = nil;
NSString * subjectParameter = @"English";
NSString * urlString = nil;



- (void)viewDidLoad
{

    [super viewDidLoad];


    //HANDLE REQUEST AUTH FOR USER CURRENT LOCATION AND SHOW USER LOCATION
    self.mapView.delegate = self;

    self.locationManager= [[CLLocationManager alloc] init];
    self.locationManager.delegate=self;

    if(IS_OS_8_OR_LATER)
    {
        [self.locationManager requestWhenInUseAuthorization];
    }


    [self.locationManager startUpdatingLocation];

    self.mapView.showsUserLocation = YES;


    //LOAD URL TO RETRIEVE ALL TEACHERS OR BY SUBJECT


    if (subjectParameter) {
        urlString = [NSString stringWithFormat: @"http://soon.nextdoorteacher.com/apps/api/nextdoorteacher/teachers?q=%@", subjectParameter];

    }else{

        urlString = [NSString stringWithFormat: @"http://soon.nextdoorteacher.com/apps/api/nextdoorteacher/teachers?q="];
    }


    // GET MY LESSONS FROM DATABASE

    NSURL *urlcc = [NSURL URLWithString:urlString];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSData *data = [NSData dataWithContentsOfURL:urlcc];
        NSError *error;
        NSMutableDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions
                                                                          error:&error];

        NSMutableArray * jsonTeach = jsonDict;
        NSLog(@"My Lessons Json == %@", jsonTeach);

        NSMutableArray *mapPointsArray = [[NSMutableArray alloc]init];
        CLLocationCoordinate2D location;
        MKPointAnnotation * myAnn;

        // LOAD ANNOTATION ARRAYS INTO: mapPointsArray
        for (int i=0; i< jsonTeach.count; i++) {

            myAnn = [[MKPointAnnotation alloc] init];

            // SET UP LOCATION
            location.latitude = [[jsonTeach[i] valueForKeyPath: @"address.latitude"]floatValue];
            location.longitude = [[jsonTeach[i] valueForKeyPath: @"address.longitude"]floatValue];
            myAnn.coordinate = location;


            //myAnn.subtitle = [jsonTeach[i] valueForKeyPath: @"rating"];
            [self.mapView addAnnotation:myAnn];



        }


    });


}



- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    // If it's the user location, just return nil.
    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    // Handle any custom annotations.
    if ([annotation isKindOfClass:[MKPointAnnotation class]])
    {
        // Try to dequeue an existing pin view first.
        MKAnnotationView *pinView = (MKAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"CustomPinAnnotationView"];
        if (!pinView)
        {
            // If an existing pin view was not available, create one.
            pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"CustomPinAnnotationView"];
            //pinView.animatesDrop = YES;
            pinView.canShowCallout = YES;
            pinView.image = [UIImage imageNamed:@"Tch4"];
            pinView.calloutOffset = CGPointMake(0, 32);
            pinView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];


            // Load and display photo using SDWEBImage

            UIImageView *photoImageView = [[UIImageView alloc] init];

            NSString *urlPhotoId = [jsonTeach [annotationIndex]valueForKeyPath:@"picture.id"];
            NSString *urlPhoto = [NSString stringWithFormat:@"http://soon.nextdoorteacher.com/img/profiles/%@.jpg", urlPhotoId];

            [photoImageView sd_setImageWithURL:[NSURL URLWithString:urlPhoto] placeholderImage:[UIImage imageNamed:@"mortarboard2"]];

            photoImageView.frame = CGRectMake(0,0,50,50);
            pinView.leftCalloutAccessoryView = photoImageView;



            pinView.tag = annotationIndex;
            annotationIndex = annotationIndex +1;


        } else {
            pinView.annotation = annotation;
        }
        return pinView;
    }
    return nil;

}

你的代码中有两个jsonTeach。如果没有 class 的全貌,我假设代码顶部的第一个是实例变量,异步块中的第二个是局部变量。

在异步块中,您将值分配给局部变量 jsonTeach,该变量将在块执行后删除。同时实例变量一保持为nil。

在异步块中更改此

NSMutableArray * jsonTeach = jsonDict;
NSLog(@"My Lessons Json == %@", jsonTeach);

self->jsonTeach = jsonDict;
NSLog(@"My Lessons Json == %@", self->jsonTeach);