2016-11-11 1 views
0

Vielleicht ist das Problem sehr seltsam, aber ich möchte wirklich wissen, wie das geht.Wie bekomme ich ViewController Wert von Klasse

Ich habe eine Klasse, die benutzerdefinierte meine Anmerkung:

-(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 i = %d", vc.i); 

     return annotationView1; 
    } 

    @end 

und die viewForAnnotation Funktion des AnnotationViewController wie folgt aus:

- (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 

Weil ich möchte 3 Anmerkungen enthalten anderes Bild machen, so dass ich erstellen Array, das Bilder eingibt.

Wenn die Annotation gemacht wird, sollte das Annotationsbild unterschiedlich sein, das ich will. So verwende ich int "i", um Array-Index zu erhalten und Bild wie das zu jeder Anmerkungen setzen:

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

und:

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

Index zum Abrufen und Festlegen Bild zu jeder Anmerkung.

Das Problem ist, wenn die App ausgeführt wird, ist das Bild der Anmerkungen gleich. drucke ich den Index und das Ergebnis wie folgt aus:

[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 

Wie Sie den Code zu sehen, "AnnotationClass i" Druck von der Annotation-Klasse und "AnnotationViewController i" ist AnnotationViewController Druckform.

Offensichtlich ist der Wert "i" bei AnnotationClass, der von AnnotationViewController aufruft, nicht wirklich der Wert selbst.

ich glaube, das Problem ist, wenn die Klasse aufrufen, wird AnnotationViewController jedes Mal init werden, so ist der Wert immer 0.

Wer kann mir helfen, das Problem zu beheben, dass, wie kann ich die AnnotationViewController Wert von Klasse bekommen?

+0

Wo ist Ihre Log-Anweisung für 'annotationClass i = 0'? –

+0

@AO, in der Annotation Class Funktion "Annotation" wie folgt: NSLog (@ "AnnotationClass i =% d", vc.i); Ich bearbeite mein Problem. – Daniel

Antwort

0

Ich denke, das Problem ist, wenn die Klasse aufrufen, wird AnnotationViewController jedes Mal init werden, so ist der Wert immer 0.

Genau richtig! Sie sagen:

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

Der ein brandneuen AnnotationViewController machen. Das ist vermutlich nicht das, was du machen willst. Was Sie tun möchten, ist beziehen Sie sich auf die AnnotationViewController, die bereits existiert.

+0

Vielen Dank. Wie kann ich den Wert von exist AnnotationViewController von der Klasse aufrufen? Ich habe wirklich keine Ahnung ... – Daniel

+0

Nun, während AnnotationViewControllers 'viewForAnnotation' ist der AnnotationViewController' self'. Jetzt müssen Sie sich also einen Weg verschaffen, das in den MKAnnotationView zu übertragen. Es ist deine Aufgabe als Programmierer vorauszuplanen, damit die Dinge, die auf andere Dinge verweisen, eine Möglichkeit haben, sie zu bekommen ... – matt

+0

OK, danke für deine Hilfe. Ich werde es versuchen :) – Daniel

0

Nach @ Matt Vorschlag finde ich die exist Viewcontroller, was ich

Code wie folgt benötigt:

Aufschalten

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

zu

AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication]delegate]; 
UITabBarController *tabVc = (UITabBarController *)delegate.window.rootViewController; 
ViewController *vc = (ViewController *)tabVc.viewControllers[0]; 

Dank @ Matt sehr viel , und hoffe, dass dies andere Person helfen kann, was das gleiche Problem hat :)

Verwandte Themen