gpt4 book ai didi

swift - 将数据从 plist 传递到 DetailView

转载 作者:行者123 更新时间:2023-11-30 13:30:37 25 4
gpt4 key购买 nike

我正在从 Objective C 转向 Swift 并重写我的应用程序。目前遇到基本问题,因此将不胜感激。

我已经设置了一个 map ,其中包含从 plist 数据中获取的注释,效果很好。我还可以将标题和副标题传递给第二个 View Controller - 没问题。

但是,我还想传递所选项目数据的所有其他字段,这就是我遇到的问题。

以前做得很好,但尽管进行了大量搜索,但找不到任何关于如何在 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 中重新创建示例后,似乎由于您从 map View 中以 MKAnnotation 形式返回所选注释,因此显然不会有 state 成员。尝试将其转换到博物馆,如下所示:

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
}

关于swift - 将数据从 plist 传递到 DetailView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36608678/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com