gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 01:37:45 25 4
gpt4 key购买 nike

我在 View 中构建一个数组并加载,然后想在 viewForAnnotation 方法中访问其中包含的一些数据,以在 map View 标注中显示图像。我已经在实现下面而不是在我的方法中声明了它,但总是发现尽管它在 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;

}

最佳答案

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

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

在异步 block 中改变这个

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

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

关于ios - 在方法之间传递数组中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32773929/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com