问题描述
我正在尝试导航到另一个页面,这是我在地图控制器视图中拥有的地图标记的详细信息页面。因此,例如,如果用户点击任何标记,则会将其带到该标记的详细信息
但是现在我面临着如何导航到下一页的问题,因为此注释不是变量。
这是我的代码:
我的子类:
class MyAnnotation: MKPointAnnotation{
var shopPID: String!
init(shopPID: String) {
self.shopPID = shopPID
}
}
db.collection("Shops").getDocuments() { (querySnapshot,err) in
if let err = err {
print("Error getting documents: \(err)")
} else {
for document in querySnapshot!.documents {
let lat = document["latitude"] as? String
let long = document["longitude"] as? String
let myFloat = (lat! as Nsstring).doubleValue
let myFloat2 = (long! as Nsstring).doubleValue
let annotation = MyAnnotation(shopPID: document["shopPID"] as! String)
annotation.coordinate = CLLocationCoordinate2D(latitude: myFloat,longitude: myFloat2)
annotation.title = document["name"] as? String
annotation.subtitle = "Click to view shop details"
annotation.shopPID = document["shopPID"] as? String
self.mapView.addAnnotation(annotation)
}
}
}
执行点击事件和点击:
func mapView(_ mapView: MKMapView,annotationView view: MKAnnotationView,calloutAccessoryControlTapped control: UIControl) {
print(#function)
if control == view.rightCalloutAccessoryView {
performSegue(withIdentifier: "mapToDetails",sender: nil)
}
}
这是我遇到的问题:我需要给注释中存储的newProjectVC赋一个值。 我该怎么办?
override func prepare(for segue: UIStoryboardSegue,sender: Any?) {
if (segue.identifier == "mapToDetails") {
let navigationController = segue.destination as! UINavigationController
let newProjectVC = navigationController.topViewController as! detailsSectionViewController
newProjectVC.getKey = //variable with each shopPID for each pin should be here
}
}
该圈子的标识符为“ mapsToDetails”
解决方法
点击注释时,应使用此MKMapViewDelegate方法设置自定义操作。
第一个子类MKA注释:
class MyAnnotation: NSObject,MKAnnotation {
var shopPID: String
var coordinate: CLLocationCoordinate2D
let title: String?
let subtitle: String?
init(shopPID: String,coordinate: CLLocationCoordinate2D,title: String,subtitle: String) {
self.shopPID = shopPID
self.coordinate = coordinate
self.title = title
self.subtitle = subtitle
}
}
然后,在您的MKMapViewDelegate上使用:
func mapView(_ mapView: MKMapView,didSelect view: MKAnnotationView) {
guard let annotation = view.annotation as? MyAnnotation else { return }
let uiStoryboard = UIStoryboard(name: "MyStoryboard",bundle: nil)
guard let vc = myStoryboard.instantiateViewController(withIdentifier: "MyViewController") as? MyViewController else { return }
vc.shopPID = annotation.shopPID
present(vc,animated: true)
}
通过这种方式,您可以识别被点击的注释并将其值传递给所需的VC。