swift2 为什么不调用ViewForAnnotation?

rsaldnfx  于 2022-11-06  发布在  Swift
关注(0)|答案(3)|浏览(205)

我知道这个问题已经问了很多次了,但是所有的答案似乎都与我的应用程序中发生的事情略有不同。
我的理解是,一旦mapView将其委托设置为ViewController(在其中显示它),并且当mapView滚动以使其显示在mapView区域内时,将向Map添加注解,则将调用viewForAnnotation函数。
目前,我有一个主视图控制器(mainVC),其中包含一个MKMapView(mapView)。该视图控制器控制四个不同的Map,以在mapView中显示。

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

有几个不同的类控制每个不同Map的功能:
1.Map1 -显示从plist读取的MKPolylines和自定义注解(继承自MKAnnotation)的组合-这工作得很好!
1.Map2 -显示从plist中读取的几条MKPolyline-这很好用!
1.Map3 -显示从plist读取的几条MKPolyline-这很好用!
1.标测图4 -应显示多个自定义注解-这不起作用!
以下是Map 4的情况:

  • MKMapView正在正确加载
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

}

  • 将mapView的委托设置为当前类(mapView.delegate = self)
  • 它会放大适当的位置(mapView.setRegion(coordinateRegion,animated:正确))
  • 该类从plist中读取,并(使用helper类)构建自定义MKAnnotations(CemAnnno)数组
  • 这些位置存储在CemAnno的一个名为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")
  • 将每个标注添加到Map(mapView.addAnnotations),并将它们显示为大头针(它们正在显示)mapView.addAnnotation(coords.allLocations![2])//这将添加一个大头针标注(可以工作),但不会调用viewForAnnotation函数

这是可行的,但是,我试图自定义正在显示的注解,但ViewForAnnotation函数从未被调用???

// 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()
}

我试过在Map的当前视图区域内添加大头针的位置,但ViewForAnnotation始终没有被触发。我试过在Map的当前视图区域外添加大头针的位置,但ViewForAnnotation始终没有被触发-我想这应该是一个有效的方法,当我“滚动”Map时,它会显示在应该触发该函数的当前视图区域内。但它没有(我应该注意到大头针正在显示,并显示在Map上)。

这使得我无法自定义我想做的引脚

我使用的技术与Map 1相同,它工作得非常好,但由于某种原因,ViewForAnnotation从未在Map 4中调用。
任何建议都将不胜感激!!

pkln4tw6

pkln4tw61#

我经常遇到这个问题。而且我发现有时候你需要像这样手动调用showAnnotations:animated:

mapView.showAnnotations(mapView.annotations, animated: true)
vq8itlhq

vq8itlhq2#

对不起,我看不到您的视图控制器mainVC的声明:您的控制器实现了MKMapViewDelegate

import MapKit

class mainVC: UIViewController, MKMapViewDelegate 
{
    ...
}

**EDIT 1:**如果在情节提要的视图控制器中,代理与控制器的链接方式如下所示:

只需右键单击mapView。

e4eetjau

e4eetjau3#

这是一个非常愚蠢的答案,但它今天咬了我两次,所以它是:检查你的纬度/经度。2如果你把它们交换了,你有一个无效的点,或者一个在海洋的中间,所以视图将永远不会被请求,因为它可能在你的可见矩形之外。

相关问题