Swift - 从地图注释中执行Segue [英] Swift - Perform Segue from map annotation
问题描述
我的地图上有1个针脚。我需要将注释的标题传递给另一个UIView(在这种情况下是ikinciEkran.swift)。但是我无法这样做。
I have 1 pin on my map. I need to pass title of annotation to another UIView (which is ikinciEkran.swift in that case). But I am unable to do so.
这是我的代码的segue部分,我不知道如何设置选定注释的标题。
Here is the segue part of my code and I do not know how to segue title of selected annotation.
我只粘贴了代码的相关部分。
I only pasted related part of the code.
class MapViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
@IBOutlet weak var mapView: MKMapView!
@IBOutlet weak var lbl_Name: UILabel!
@IBOutlet weak var lbl_Address: UILabel!
@IBOutlet weak var lbl_Phone: UILabel!
var LocationManager = CLLocationManager()
var i = 0
var annotation:MKAnnotation!
override func viewDidLoad() {
super.viewDidLoad()
LocationManager.delegate = self
mapView.delegate = self
LocationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
LocationManager.requestWhenInUseAuthorization()
LocationManager.startUpdatingLocation()
let tekel1 = TekelClass(
title: "tekel1",
locationName: "Tekelİsim1",
coordinate: CLLocationCoordinate2D(latitude: 40.9400, longitude: 29.300520))
mapView.addAnnotation(tekel1)
}
func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!,
calloutAccessoryControlTapped control: UIControl!) {
if control == view.rightCalloutAccessoryView {
performSegueWithIdentifier("toTheMoon", sender: self)
}
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "toTheMoon" )
{
var ikinciEkran = segue.destinationViewController as! DetailViewController
ikinciEkran.tekelName = "how to segue title of the selected pin"
}
}
推荐答案
您需要实现 viewForAnnotation
方法。
示例。
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
if annotation is MKUserLocation {
return nil
}
let reuseId = "pin"
var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
if pinView == nil {
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView?.canShowCallout = true
var rightButton: AnyObject! = UIButton.buttonWithType(UIButtonType.DetailDisclosure)
rightButton.titleForState(UIControlState.Normal)
pinView!.rightCalloutAccessoryView = rightButton as! UIView
}
else {
pinView?.annotation = annotation
}
return pinView
}
如果用户点击注释上的(i)按钮,将调用 calloutAccessoryControlTapped
。
If user tap (i) button on the annotation, calloutAccessoryControlTapped
will be called.
ref。我的MKMapView示例代码
https://github.com/koogawa/MKMapViewSample
ref. My MKMapView sample code https://github.com/koogawa/MKMapViewSample
编辑
func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
if control == view.rightCalloutAccessoryView {
performSegueWithIdentifier("toTheMoon", sender: view)
}
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "toTheMoon" )
{
var ikinciEkran = segue.destinationViewController as! DetailViewController
ikinciEkran.tekelName = (sender as! MKAnnotationView).annotation!.title
}
}
这篇关于Swift - 从地图注释中执行Segue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!