我正在构建一个数组,在视图中加载,然后想要访问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;
}
答案 0 :(得分:1)
您的代码中有两个jsonTeach。如果没有类的全貌,我假设代码顶部的第一个是实例变量,而async块中的第二个是局部变量。
在异步块中,您将值分配给局部变量jsonTeach,该变量将在块执行后删除。同时,实例变量1保持为零。
在异步块中更改此
NSMutableArray * jsonTeach = jsonDict;
NSLog(@"My Lessons Json == %@", jsonTeach);
到
self->jsonTeach = jsonDict;
NSLog(@"My Lessons Json == %@", self->jsonTeach);