Swift - 从地图注释中执行 Segue
Posted
技术标签:
【中文标题】Swift - 从地图注释中执行 Segue【英文标题】:Swift - Perform Segue from map annotation 【发布时间】:2015-10-10 12:11:51 【问题描述】:我的地图上有 1 个图钉。我需要将注释的标题传递给另一个 UIView(在这种情况下是 ikinciEkran.swift)。但我做不到。
这是我的代码的 segue 部分,我不知道如何 segue 所选注释的标题。
我只粘贴了相关的部分代码。
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"
【问题讨论】:
【参考方案1】:你需要实现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
将被调用。
参考。我的 MKMapView 示例代码 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
【讨论】:
亲爱的 Kosuke,如何分割 pin 的标题?在您的 github 示例中,没有任何传递的内容。以上是关于Swift - 从地图注释中执行 Segue的主要内容,如果未能解决你的问题,请参考以下文章
如何在 xctest 中使用 swift 点击地图注释时访问标注视图
Swift 中的自定义 MKAnnotation 未添加到地图的注释中