Warum ist ViewForAnnotation nicht genannt?

Ich weiß, diese Frage wurde gefragt, mehrere Male, jedoch haben Sie alle die Antworten scheinen etwas anders als das, was ist Los in meinem app.

Mein Verständnis ist, dass die viewForAnnotation Funktion wird aufgerufen, sobald die mapView hat seine Delegierten gesetzt, um den ViewController in dem es angezeigt wird, UND eine Anmerkung Hinzugefügt wird, die anzeigen, wenn der Kartenansicht scrollt, so dass es zeigt, innerhalb der mapView region.

Derzeit habe ich eine Haupt-viewController (mainVC) mit einer MKMapView (mapView)
Dieser viewController steuert vier verschiedene Karten zur Darstellung in mapView dargestellt werden.

func moveViews(sender:Int) {
    //This function handles which button on the Segmented Control was clicked and the loads the appropriate map into the mapView (passed as a para

    removeAnnotationsAndOverlays() //~~~
    if sender == 0 {
        //First Map was selected
        let map1VC = map1VC()
        map1VC.loadMap1View(mapView)
        map1VC.centerMapOnLocation()
    }
    else if sender == 1 {
        //Second Map was selected
        let map2VC = map2VC()
        map2VC.loadMap2View(mapView)
    }
    else if sender == 2 {
        //Third Map was selected
        let map3VC = map3VC()
        map3VC.loadMap3View(mapView)
    }
    else if sender == 3 {
        //Fourth Map was selected
        let map4VC = map4VC()
        map4VC.loadMap4View(mapView)
    }
    else {
        //Load First Map as default
        let map1VC = map1VC()
        map1VC.loadMap1View(mapView)
        map1VC.centerMapOnLocation()
    }
}

Gibt es verschiedene Klassen, welche Steuerelemente, die die Funktionalität von jeder der verschiedenen Karten:

  1. Karte 1 - Zeigt eine Kombination von MKPolylines und individuelle Anmerkungen (erbt von MKAnnotation), die gelesen werden, aus einer plist - Das funktioniert Super!
  2. Karte 2 - Zeigt mehrere MKPolylines Lesen aus einer plist - Das funktioniert Super!
  3. Karte 3 - Zeigt mehrere MKPolylines Lesen aus einer plist - Das funktioniert Super!
  4. Karte 4 - angezeigt werden Sollen, die mehrere individuelle Anmerkungen - das wird NICHT funktionieren!

Hier ist, was passiert mit Karte 4:

  • die MKMapView ist ordnungsgemäß geladen

    var mapView: MKMapView = MKMapView() //declared as a global variable/object inside the map4VC()
    
    //Initial function to set up Map
    //     Think of this function as the "override func viewDidLoad() {}"
    func loadMap4View(mV: MKMapView) {
    
    //This connects the parameter passed (mV) and sets it as the delegate for the mapView used throughout this file
    //     In other words, it allows the mapView on the MainVC to use all of the code in this file to handle different actions
    mapView = mV
    mapView.delegate = self
    
    let initialLocation = CLLocation(latitude: 50.3603125, longitude: 2.794017)
    
    //calculates the region you'll look at on the screen 
    let coordinateRegion = MKCoordinateRegionMakeWithDistance(initialLocation.coordinate, regionRadius, regionRadius)
    
    //sets the region of the map
    mapView.setRegion(coordinateRegion, animated: true)
    
    
    //addPins()  //This can use a custom function to load all of the Pins
    //mapView.addAnnotations(coords.allLocations!) //This line also works to add all of the individual pins
    
    mapView.addAnnotation(coords.allLocations![2]) // This adds one single Pin

    }

  • legen Sie die mapView die Delegierten auf die aktuelle Klasse (mapView.delegate = self)

  • es zoomt in die richtige Position (mapView.setRegion(coordinateRegion, animated: true))
  • die Klasse liest aus einer plist und (mit einem Helfer-Klasse) baut ein array mit benutzerdefinierten MKAnnotations (CemAnno)

  • Die Standorte gespeichert sind in ein array von CemAnno heißt allLocations:

    var allLocations: [CemAnno]? = [] //This is declared in the helper class along with a bunch of other stuff that grabs all of the information from a plist
    
     class CemAnno: NSObject, MKAnnotation {
    
    var coordinate: CLLocationCoordinate2D
    var title: String?
    var casualties: String?
    var visitInfo: String?
    var histInfo: String?
    var region: String?
    
    
    init(title: String, coordinate: CLLocationCoordinate2D, region: String, casualties: String, visitInfo: String, histInfo: String) {
    
    self.coordinate = coordinate
    self.title = title
    self.region = region
    self.casualties = casualties
    self.visitInfo = visitInfo
    self.histInfo = histInfo
    }

    }

    //This builds an object inside the the map4VC() class called coords that holds all of the information collected from the plist
    var coords = BuildCoordinates(filename: "Coordinate")
  • fügt jeder dieser auf der Karte (Kartenansicht.addAnnotations), und Sie sind dargestellt in Form von pins (Sie angezeigt werden)
    mapView.addAnnotation(coords.allLocations![2]) //Dies fügt ein single-Pin-Annotation (was funktioniert), aber nie ruft die viewForAnnotation Funktion

Dies funktioniert, aber ich versuche, passen Sie die Beschriftungen werden angezeigt, aber der ViewForAnnotation Funktion wird nie aufgerufen????

//This function is NEVER called
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView?
{
    //Define a reuse identifier. This is a string that will be used to ensure we reuse annotation views as much as possible.
    let identifier = "CemAnno"

    //Check whether the annotation we're creating a view for is one of our CemAnno objects.
    if annotation.isKindOfClass(CemAnno.self) {
        print("correct class")
        //let anno = annotation as! CustomAnnotation
        //Try to dequeue an annotation view from the map view's pool of unused views.
        var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)

        if annotationView == nil {
            print("no reusable view")
            //If it isn't able to find a reusable view, create a new one using MKPinAnnotationView and sets its canShowCallout property to be true. This triggers the popup with the name.
            annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
            annotationView!.canShowCallout = true

            //Create a new UIButton using the built-in .Custom type. This is so we can add an image to the button.
            let btn = UIButton(type: .DetailDisclosure)
            //btn.setImage(anno.image, forState: .Normal)
            annotationView!.rightCalloutAccessoryView = btn
        } else {
            print("reusing a view")
            //If it can reuse a view, update that view to use a different annotation.
            annotationView!.annotation = annotation
        }

        return annotationView

    }

    //If the annotation isn't from a CustomClass, it must return nil so iOS uses a default view.
    return MKPinAnnotationView()
}

Ich habe versucht, indem die Lage des einen pin in der aktuellen Ansicht Bereich der Karte, aber die ViewForAnnotation wird nie gefeuert.
Ich habe versucht, indem die Lage des Stiftes außerhalb der aktuellen Ansicht Bereich der Karte, aber die ViewForAnnotation ist nie abgefeuert - ich dachte, dies sollte eine, die funktioniert, und als ich 'Blättern' die Karte, und es zeigt sich in der aktuellen Ansicht-region, der das Feuer der Funktion, aber es funktioniert nicht (ich sollte beachten Sie, dass die pin wird angezeigt, und zeigt auf die Karte).

Dies macht es so, dass ich nicht anpassen, die pin, die ich gerne tun würde.

Bin ich mit der gleichen Technik wie Karte 1 funktioniert perfekt in Ordnung, aber für einige Grund der ViewForAnnotation wird nie aufgerufen, in Anzeigen 4.

Anregungen würde sehr geschätzt werden!!

  • etwas code könnte helfen
  • Ich habe code Hinzugefügt. Nun merke ich, wie ich ändern sich von Karte 1 auf Map4 zurück zur Karte 1 nur einen Teil der Zeit sind meine benutzerdefinierte Annotationen zeigt sich mit den Bildern, die ich will. Andere Male werden Sie nur angezeigt wird, mit einem pin.
  • Ich habe gespielt, um mit mehreren Dingen, und ich denke, es muss etwas zu tun mit dem delegieren. Obwohl die Stellvertretung eingestellt wird, ist es, als wenn es nicht richtig eingestellt, um die Instanz von MKMapView, die angezeigt wird (trotz festgelegt wird, in der ersten Zeile des func-loadMap4View(mV: MKMapView) - Methode). Könnte es damit etwas zu tun haben mit der mV parameter übergeben wird in eine separate Klasse (by reference übergeben oder Typ)??? Irgendwelche Ideen?
  • Komisch Funktionalität: Auf der MainVC die verschiedenen Karten ausgewählt sind, die durch die Verwendung eines Segmentierten Kontrolle. Wie ich schnell drücken auf verschiedene Tasten, um zu laden, die verschiedene Karten in etwa 10% der Zeit, die das Programm bricht und schlägt den Haltepunkt in der ViewForAnnotation-Methode (die ist, was ich will, geschehen). Warum nur 10% der Zeit? Ich kann nicht replizieren, unter bestimmten Umständen, es scheint völlig zufällig.
  • Wenn Sie das hinzufügen von Annotationen in AsyncQueue plz hinzufügen von Anmerkungen in mainQueue. stackoverflow.com/questions/3821515/...
  • Wie wissen Sie, ob Sie das hinzufügen von Annotationen in AsynQueue? Ist das, wo Anmerkungen Hinzugefügt werden, die standardmäßig?

InformationsquelleAutor M. Black | 2016-03-27
Schreibe einen Kommentar