问题描述
我希望将信息从Pin批注传递到另一个viewController.我可以传递批注的标题和副标题,但我需要将一些额外的信息与这些信息一起传递.除了标题和副标题之外,是否可以将其他信息添加到MKPointAnnotation?
I wish to pass informations from a Pin annotation to another viewController.I can pass the title and subtitle of the annotation but I need to pass some extra information along with these. Is there a way to add extra information to a MKPointAnnotation other than just title and subtitle?
在这里设置了图钉标题和副标题,以便它们出现在地图上:
here I have the pin title and subtitle set so it appears on the map:
var zoopin = MKPointAnnotation()
zoopin.coordinate = zoo
zoopin.title = "The zoo"
zoopin.subtitle = "hello this is the zoo"
mapView.addAnnotation(zoopin)
标题和副标题随后通过以下方式传递到我的信息视图控制器:
the title and subtitle are then passed to my info view controller using:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "info") {
if let annotation = sender as? MKAnnotationView {
let detailViewController = segue.destinationViewController as! info
detailViewController.titleText = annotation.annotation?.title ?? ""
detailViewController.detaileText = annotation.annotation?.subtitle ?? ""
}
}
}
推荐答案
创建自己的注释,新文件或类
make your own annotation, new file or class
import MapKit
class MyAnnotation: NSObject, MKAnnotation {
var coordinate: CLLocationCoordinate2D
var EXTRA_INFORMATION: String?
var title: String?
init(coordinate: CLLocationCoordinate2D) {
self.coordinate = coordinate
}
}
并使用它代替普通的MKPointAnnotation
and use it instead of normal MKPointAnnotation
var zoopin = MyAnnotation()
zoopin.coordinate = zoo
zoopin.title = "The zoo"
zoopin.subtitle = "hello this is the zoo"
zoopin.EXTRA_INFORMATION = "that is your new extra info that you wanted to add?"
mapView.addAnnotation(zoopin)
这篇关于向标题和副标题之外的其他细节添加到MKPointAnnotation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!