我知道以前已经问过很多类似的问题。只是找不到适合我需要的解决方案。请帮助
我一直试图将Firebase中的信息加载到mapview上。我能够附加所有信息视图图注解视图。但是,我要做的是单击地图注释视图时将Firebase数据传递给新的ViewController。单击批注时,我可以选择另一个ViewController,但加载了错误的Firebase数据
到目前为止我尝试过的代码:
override func viewDidLoad() {
super.viewDidLoad()
self.mapView.delegate = self
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.requestLocation()
observeGeofire()
}
func observeGeofire() {
let dataRef = Database.database().reference().child("Shoutout")
dataRef.observeSingleEvent(of: .value, with: { (snapshot) in
for snap in snapshot.children {
let postSnap = snap as! DataSnapshot
if let dict = postSnap.value as? [String: AnyObject] {
let Lat = dict["lat"] as! CLLocationDegrees
let Long = dict["long"] as! CLLocationDegrees
let EVENT = dict["Event Type"] as? String
let Radmap = self.Rad
var annotations = MKPointAnnotation()
annotations.coordinate = CLLocationCoordinate2D(latitude: Lat, longitude: Long)
annotations.title = dict["title"] as? String
let loc = CLLocation(latitude: Lat, longitude: Long)
let distanceFromUser = (self.Location.distance(from: loc))/1000
let span = MKCoordinateSpanMake(0.25, 0.25)
if distanceFromUser < Radmap && EVENT! == self.sovcCET {
self.mapView.addAnnotation(annotations)
print("testing text for \(self.sovcCET)")
} else if distanceFromUser < Radmap && self.sovcCET == "All Events"{
self.mapView.addAnnotation(annotations)
} else if distanceFromUser < Radmap && self.sovcCET == "Choose Event" {
self.mapView.addAnnotation(annotations)
} else {
print("failed test")
}
}
}
})
}
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?{
if annotation is MKUserLocation {
//return nil so map view draws "blue dot" for standard user location
return nil
}
let reuseId = "pin"
var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) as? MKPinAnnotationView
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView?.pinTintColor = UIColor.red
pinView?.canShowCallout = true
let smallSquare = CGSize(width: 30, height: 30)
let button = UIButton(frame: CGRect(origin: CGPoint(x: 0,y :0), size: smallSquare))
let rightButton = UIButton(type: .contactAdd)
rightButton.tag = annotation.hash
pinView?.animatesDrop = true
pinView?.canShowCallout = true
pinView?.rightCalloutAccessoryView = rightButton
pinView?.leftCalloutAccessoryView = button
return pinView
}
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView,
calloutAccessoryControlTapped control: UIControl) {
//mapToDetails
if control == view.rightCalloutAccessoryView {
performSegue(withIdentifier: "mapToDetails", sender: self)
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "mapToDetails" {
if let destination = segue.destination as? shoutoutDetailViewController {
destination.shoutoutSelected = ShoutoutSelected
}
}
}
再次感谢
您可以使用下面的代码来代替segue
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView,
calloutAccessoryControlTapped control: UIControl) {
let vc = self.storyboard?.instantiateViewController(withIdentifier:
"DestinationViewController") as! DestinationViewController
vc.shoutoutSelected = ShoutoutSelected
self.navigationController?.pushViewController(vc, animated: true)
}
[当使用performSegue,performSegue(withIdentifier: "mapToDetails", sender: self)
时,您可以在sender
中发送对象。因此,不要发送sender : self
,请尝试sender: ShoutoutSelected
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView,
calloutAccessoryControlTapped control: UIControl) {
//mapToDetails
if control == view.rightCalloutAccessoryView {
performSegue(withIdentifier: "mapToDetails", sender: ShoutOutSelected)
}
}
现在prepareForSegue,
中,您可以检索sender
的值并将其传递给目标控制器。
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "mapToDetails" {
if let destination = segue.destination as? shoutoutDetailViewController {
destination.shoutoutSelected = sender as! Int // here sender = ShoutoutSelected, downcast it from Any to your desired type. I am considering destination.shoutoutSelected is of Int type.
}
}
}