Daniel Daniel - 22 days ago 7
iOS Question

How to get ViewController Value From Class

Maybe the problem is very strange, but I really want to know how to do that.

I have a class that custom my annotation:

-(MKAnnotationView *)annotation
{
NSArray *imageArray = [[NSArray alloc] initWithObjects:@"pin1",@"pin2",@"pin3", nil];

AnnotationViewController *vc = [[AnnotationViewController alloc] initWithNibName:@"AnnotationViewController" bundle:nil];

MKAnnotationView *annotationView1 = [[MKAnnotationView alloc] init];

int j = 0;
while (j < 1) {

annotationView1.frame = CGRectMake(0, 0, 30, 30);
annotationView1.canShowCallout = true;

MKAnnotationView *annotationView2 = [[MKAnnotationView alloc] initWithAnnotation:self reuseIdentifier:@"annotation"];

annotationView2.image = [UIImage imageNamed:imageArray[vc.i]];
annotationView2.frame = CGRectMake(0, 0, 30, 30);
annotationView2.layer.cornerRadius = 15;
annotationView2.layer.masksToBounds = true;
[annotationView1 addSubview:annotationView2];

NSLog(@"AnnotationClass i = %d", vc.i);
j += 1;
}
return annotationView1;
}

@end


and the AnnotationViewController's viewForAnnotation function like that:

- (nullable MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[Annotation class]]) {

// Custom annotation
Annotation *customAnnotaton = [[Annotation alloc] init];
MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"annotation"];

if (annotationView == nil) {
annotationView = customAnnotaton.annotation;
self.i += 1;
NSLog(@"AnnotationViewController i = : %d", self.i);
}
return annotationView;
}
return nil;
}

@end


Because I want to make 3 annotations include different image, so I create a array that input images.

When the annotation be make, the annotation image should be different each other that I want. So I use int "i" to get array index and put image to each annotations like that:

AnnotationViewController *vc = [[AnnotationViewController alloc] initWithNibName:@"AnnotationViewController" bundle:nil];


and :

annotationView2.image = [UIImage imageNamed:imageArray[vc.i]];


to get index and set image to each annotation.

The problem is, when run the app, the annotations image is same.
I print the index and the result like that:

[7783:2749915] AnnotationClass i = 0
[7783:2749915] AnnotationViewController i = 1
[7783:2749915] AnnotationClass i = 0
[7783:2749915] AnnotationViewController i = 2
[7783:2749915] AnnotationClass i = 0
[7783:2749915] AnnotationViewController i = 3


As you see the code, "AnnotationClass i" is print from the Annotation Class, and "AnnotationViewController i" is print form AnnotationViewController.

Obviously, the value "i" at AnnotationClass that call from AnnotationViewController is not really the value itself.

I think the problem is when call the class, AnnotationViewController will be init each time, so the value is always 0.

Anyone can help me fix the problem that how can I get the AnnotationViewController's value from Class?

Answer

I think the problem is when call the class, AnnotationViewController will be init each time, so the value is always 0.

Exactly right! You are saying:

AnnotationViewController *vc = [[AnnotationViewController alloc] initWithNibName:@"AnnotationViewController" bundle:nil];

That makes a brand new AnnotationViewController. That is presumably not what you want to do. What you want to do is refer to the AnnotationViewController that already exists.

Comments