除了标题和副标题之外,向 MKPointAnnotation 添加额外的细节
Add extra detail to MKPointAnnotation other than title and subtitle
我希望将信息从 Pin 注释传递到另一个 viewController.I 可以传递注释的标题和副标题,但我需要传递一些额外的信息。除了标题和副标题之外,有没有办法向 MKPointAnnotation 添加额外信息?
这里我设置了图钉标题和副标题,因此它显示在地图上:
var zoopin = MKPointAnnotation()
zoopin.coordinate = zoo
zoopin.title = "The zoo"
zoopin.subtitle = "hello this is the zoo"
mapView.addAnnotation(zoopin)
然后将标题和副标题传递到我的信息视图控制器,使用:
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 ?? ""
}
}
}
创建自己的注释、新文件或class
import MapKit
class MyAnnotation: NSObject, MKAnnotation {
var coordinate: CLLocationCoordinate2D
var EXTRA_INFORMATION: String?
var title: String?
init(coordinate: CLLocationCoordinate2D) {
self.coordinate = coordinate
}
}
并使用它代替普通的 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)
我希望将信息从 Pin 注释传递到另一个 viewController.I 可以传递注释的标题和副标题,但我需要传递一些额外的信息。除了标题和副标题之外,有没有办法向 MKPointAnnotation 添加额外信息?
这里我设置了图钉标题和副标题,因此它显示在地图上:
var zoopin = MKPointAnnotation()
zoopin.coordinate = zoo
zoopin.title = "The zoo"
zoopin.subtitle = "hello this is the zoo"
mapView.addAnnotation(zoopin)
然后将标题和副标题传递到我的信息视图控制器,使用:
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 ?? ""
}
}
}
创建自己的注释、新文件或class
import MapKit
class MyAnnotation: NSObject, MKAnnotation {
var coordinate: CLLocationCoordinate2D
var EXTRA_INFORMATION: String?
var title: String?
init(coordinate: CLLocationCoordinate2D) {
self.coordinate = coordinate
}
}
并使用它代替普通的 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)