【发布时间】:2015-12-22 19:10:46
【问题描述】:
我正在视图中构建一个数组并加载,然后想要访问 viewForAnnotation 方法中包含的一些数据,以在地图视图标注中显示图像。我已经在实现下方声明了它,但没有在我的方法中声明它,但总是发现尽管它在 ViewDidLoad 中正确加载和处理,但在 viewForAnnotation 方法中它始终为空。我在其他程序中使用了类似的变量设置,但不明白为什么它在这里不起作用,对不起,我对此很陌生,它可能很简单,我错过了:-/
我的问题很简单,我如何才能将 viewDidLoad 中加载的数据访问到 viewForAnnotation 方法中的 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;
}
【问题讨论】:
标签: ios objective-c arrays variables instance-variables