【发布时间】:2016-04-13 20:14:30
【问题描述】:
我正在从 Objective C 迁移到 Swift 并重写我的应用程序。目前遇到基本问题的问题,因此将不胜感激。
我已经设置了一个带有从 plist 数据中提取的注释的地图,效果很好。我还可以将标题和副标题传递给第二个视图控制器——没问题。
但是,我也想传递所选项目数据的所有其他字段,这就是我遇到的问题。
之前做得很好,但尽管进行了大量搜索,但找不到任何关于如何在 Swift 中执行此操作的指示。一个例子是完美的:-)
import MapKit
class Museums: NSObject, MKAnnotation {
var title: String?
var subtitle: String?
var state: String?
var latitude: Double
var longitude:Double
var coordinate: CLLocationCoordinate2D {
return CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
}
init(latitude: Double, longitude: Double) {
self.latitude = latitude
self.longitude = longitude
}
}
class ViewController: UIViewController, MKMapViewDelegate {
@IBOutlet weak var mapView: MKMapView!
var infoToPass: String?
//View did load
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self
zoomToRegion()
let annotations = getMapAnnotations()
// Add mappoints to Map
mapView.addAnnotations(annotations)
mapView.delegate = self
}
//Zoom to region
func zoomToRegion() {
let location = CLLocationCoordinate2D(latitude: 39.8, longitude: -98.6)
let region = MKCoordinateRegionMakeWithDistance(location, 3150000.0, 3150000.0)
mapView.setRegion(region, animated: true)
}
// Annotations
func getMapAnnotations() -> [Museums] {
var annotations:Array = [Museums]()
//load plist file
var myMuseums: NSArray?
if let path = NSBundle.mainBundle().pathForResource("MyAnnotationsUSA", ofType: "plist") {
myMuseums = NSArray(contentsOfFile: path)
}
if let items = myMuseums {
for item in items {
let lat = item.valueForKey("latitude") as! Double
let long = item.valueForKey("longitude")as! Double
let annotation = Museums(latitude: lat, longitude: long)
annotation.title = item.valueForKey("title") as? String
annotation.state = item.valueForKey("state") as? String
annotations.append(annotation)
}
}
return annotations
}
func mapView(mapView: MKMapView, viewForAnnotation annotations: MKAnnotation) -> MKAnnotationView? {
let reuseIdentifier = "pin"
var pin = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseIdentifier) as? MKPinAnnotationView
if pin == nil {
pin = MKPinAnnotationView(annotation: annotations, reuseIdentifier: reuseIdentifier)
// pin!.pinColor = .Red
pin!.canShowCallout = true
pin!.rightCalloutAccessoryView = UIButton(type: .DetailDisclosure)
} else
{
pin!.annotation = annotations
}
return pin
}
func mapView(mapView: MKMapView, annotationView: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
if control == annotationView.rightCalloutAccessoryView {
self.performSegueWithIdentifier("showDetail", sender: self)
}
}
//prepare for segue
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if self.mapView.selectedAnnotations.count == 0 {
//no annotation selected
return;
}
let titleToPass = self.mapView.selectedAnnotations[0] //as? MKAnnotation
print("\(infoToPass.title!)")
let destination = segue.destinationViewController as! DetailViewController
//This works
destination.myLabelText = infoToPass.title!
// This does not
destination.myStateText = infoToPass.state
}
}
【问题讨论】:
-
只是对您的示例代码的风格评论:过多的空格和不一致的缩进有点伤害我的大脑:-)
-
杰尔。哎呀。对不起。希望你的大脑已经恢复 :-)
标签: swift dictionary annotations plist