gpt4 book ai didi

swift - 即使使用 Swift 正确添加了 MKPinAnnotationView,MKPinAnnotationView 也不会显示在 MKMapView 中

转载 作者:行者123 更新时间:2023-11-30 12:06:14 26 4
gpt4 key购买 nike

我正在开发一个 OSX 应用程序。

我对 MKAnnotation 进行了子类化:

import Foundation
import MapKit

class LocationAnnotation: NSObject, MKAnnotation {

var image: NSImage?
var title: String?
var coordinate: CLLocationCoordinate2D

init(image anImage: NSImage?, title aTitle: String, coordinate aCoordinate: CLLocationCoordinate2D) {
self.image = anImage
self.title = aTitle
self.coordinate = aCoordinate
}
}

在我的 NSViewController 子类中,我添加了一个 MKMapViewDelegate,并在 Interface Builder 中添加了一个 MKMapView 并将其委托(delegate)设置为 NSViewController。

为了找出问题所在,我将 ViewDidLoad 方法中的三个位置添加到一个数组中。我将这些注释添加到 ViewDidAppear 中的 map 中。当我找出问题所在时,我计划将其移至后台线程。

import Cocoa
import MapKit

class ShowLocationsViewController: NSViewController, MKMapViewDelegate {

@IBOutlet var locationMap: MKMapView!

private var myLocationArray: [LocationAnnotation] = []
private var myRegion: MKCoordinateRegion!

//#MARK: - UIViewController

override func viewDidLoad() {
super.viewDidLoad()

myLocationArray = []
myRegion = nil

let locLondon = LocationAnnotation(image: nil, title: "London", coordinate: CLLocationCoordinate2DMake(51.522617, -0.139371))
let locWembley = LocationAnnotation(image: nil, title: "Wembley", coordinate: CLLocationCoordinate2DMake(51.555909, -0.279600))
let locGreenwich = LocationAnnotation(image: nil, title: "Greenwich", coordinate: CLLocationCoordinate2DMake(51.476572, -0.001596))

myLocationArray.append(locLondon)
myLocationArray.append(locWembley)
myLocationArray.append(locGreenwich)

myRegion = MKCoordinateRegion.init(center: locLondon.coordinate, span: MKCoordinateSpanMake(0.20, 0.20)) // 20km span
}

override func viewDidAppear() {
locationMap.addAnnotations(myLocationArray)
locationMap.setRegion(myRegion, animated: true)
}
}

当使用委托(delegate)的方法viewFor注释时:我打印出日志中每个注释的标题,并且列出了所有三个注释。在另一个委托(delegate)的方法 didAdd 中,我在日志中打印出 map 注释的数量,它打印出三个。但在我的 map 上,没有显示注释。我尝试平移和缩放但没有成功。不过,区域已正确设置和显示。

//#MARK: - MKMapViewDelegate

func mapView(_ mapView: MKMapView, didAdd views: [MKAnnotationView]) {
print(locationMap.annotations.count, views.count)
}

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is LocationAnnotation {
let annotationIdentifier = "Location"

var annotationView = locationMap.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) as! MKPinAnnotationView?
if annotationView == nil {
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
}

annotationView!.canShowCallout = true

print(annotationView!.annotation!.title ?? "no view")

return annotationView
} else {
print("no annotation")
return nil
}
}

当我在 iOS 应用程序中尝试相同的代码时,一切正常。我认为我的委托(delegate)没有任何问题,因为方法被正确调用。

如果您能给我任何帮助,我将不胜感激。

最佳答案

我找出了问题所在,即调用窗口的方式。

我在 AppDelegate 中创建了一个变量 mainWindowController,然后我对主 NSViewController 进行了子类化,以便我可以将 AppDelegate 连接到它。

let appDelegate = NSApp.delegate as! AppDelegate
appDelegate.mainWindowController = self

后来我用这段代码调用我的窗口,这是错误的:

let mapViewController = mainWindowController?.storyboard?.instantiateController(withIdentifier: NSStoryboard.SceneIdentifier.init(rawValue: "ShowLocationsID")) as! ShowLocationsViewController
mainWindowController?.contentViewController?.presentViewControllerAsModalWindow(mapViewController)

我改变了这一点,在我的 Storyboard中创建了一个segue,并调用了以下有效的代码,并且我的引脚确实显示了:

performSegue(withIdentifier: NSStoryboardSegue.Identifier(rawValue: "ShowLocations"), sender: self)

关于swift - 即使使用 Swift 正确添加了 MKPinAnnotationView,MKPinAnnotationView 也不会显示在 MKMapView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46641587/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com