将数据从plist传递给DetailView

时间:2016-04-13 20:14:30

标签: swift dictionary annotations plist

我正在从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

    }
}

1 个答案:

答案 0 :(得分:0)

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
}