ios - How to get ViewController Value From Class -
maybe problem strange, want know how that.
i have class custom 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]; 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 = %d", vc.i); return annotationview1; } @end and annotationviewcontroller's viewforannotation function 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 = : %d", self.i); } return annotationview; } return nil; } @end because want make 3 annotations include different image, create array input images.
when annotation make, annotation image should different each other want. use int "i" array index , put image each annotations that:
annotationviewcontroller *vc = [[annotationviewcontroller alloc] initwithnibname:@"annotationviewcontroller" bundle:nil]; and :
annotationview2.image = [uiimage imagenamed:imagearray[vc.i]]; to index , set image each annotation.
the problem is, when run app, annotations image same. print index , result that:
[7783:2749915] annotationclass = 0 [7783:2749915] annotationviewcontroller = 1 [7783:2749915] annotationclass = 0 [7783:2749915] annotationviewcontroller = 2 [7783:2749915] annotationclass = 0 [7783:2749915] annotationviewcontroller = 3 as see code, "annotationclass i" print annotation class, , "annotationviewcontroller i" print form annotationviewcontroller.
obviously, value "i" @ annotationclass call annotationviewcontroller not value itself.
i think problem when call class, annotationviewcontroller init each time, value 0.
anyone can me fix problem how can annotationviewcontroller's value class?
i think problem when call class, annotationviewcontroller init each time, value 0.
exactly right! saying:
annotationviewcontroller *vc = [[annotationviewcontroller alloc] initwithnibname:@"annotationviewcontroller" bundle:nil]; that makes brand new annotationviewcontroller. presumably not want do. want refer annotationviewcontroller already exists.
Comments
Post a Comment