我正在从Objective C迁移到Swift,并重写了我的Apps。目前遇到一些基本问题,希望能对您有所帮助。
我已经建立了一个地图,该地图带有从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
}
}
最佳答案
在您的DetailViewController
中,您应该声明保存其他数据的属性。然后,像设置prepareForSegue
一样在myLabelText
方法中设置这些属性。除了两种语言之间的明显差异外,这实际上应该与Objective-C没什么不同。
或者,如果还有其他事情,而不是显而易见的事情,请在您的问题中添加更多信息。
更新:在查看您的注释并在Xcode中重新创建了示例后,似乎由于从地图视图中以MKAnnotation
的形式返回选定的注释,因此显然没有state
成员。尝试将其转换为Museums
,如下所示:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if self.mapView.selectedAnnotations.count == 0 {
//no annotation selected
return
}
let infoToPass = self.mapView.selectedAnnotations[0] as! Museums // <-- NOTE
let destination = segue.destinationViewController as! DetailViewController
destination.myLabelText = infoToPass.title!
destination.myStateText = infoToPass.state
}