gpt4 book ai didi

ios - 将图像从 map 注释传递到详细信息 View Controller

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

我在单独的 swift 文件中有一个名为 Capital 的类。在我的(ViewController)中,我为 map 上的每个图钉声明位置、图像、标题等。当点击 pin 时,会弹出 calloutAccessoryControlTapped 并将我定向到我的 DetailsViewController,我在其中传递了点击的 pin 的标题,但无法传递图像...

    let Headquarters = Capital(title: "Headquarters", coordinate: CLLocationCoordinate2D(latitude: (some latitude), longitude: (some longitude) ), info: "Our headquarters", image: "Location1")  

然后我就可以完美地从 ViewController 到 DetailViewController

      override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showDetails" {

let theDestination:DetailsViewController = segue.destinationViewController as! DetailsViewController
theDestination.toPass = (sender as! MKAnnotationView).annotation!.title! } }

我的类(class)资本

    import MapKit
import UIKit

class Capital: NSObject, MKAnnotation {

var title: String?
var coordinate: CLLocationCoordinate2D
var info: String
var image: String?


init(title: String, coordinate: CLLocationCoordinate2D, info: String, image: String) {
self.title = title
self.coordinate = coordinate
self.info = info
self.image = image
}
}

这就是我在 ViewController 中的方式(声明它们)

    let Headquarters = Capital(title: "Headquarters", coordinate: CLLocationCoordinate2D(latitude: (some coordinate), longitude: (some coordinate)), info: "Our headquarters", image: "Location1")   

还有更多

    mapView.addAnnotations([Headquarters, BlahBlah, OtherStuff])

然后我把它们放进别针

      func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
let identifier = "Capital"

if annotation.isKindOfClass(Capital.self) {

var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)

if annotationView == nil {

annotationView = MKAnnotationView(annotation:annotation, reuseIdentifier:identifier)
annotationView!.canShowCallout = true
annotationView!.image = UIImage(named: "pin")


let btn = UIButton(type: .DetailDisclosure)
annotationView!.rightCalloutAccessoryView = btn
} else {

annotationView!.annotation = annotation

}

let cpa = annotation as! Capital
annotationView!.image = UIImage(named:cpa.image!)

return annotationView

}


return nil
}

然后我尝试将图像传递给DetailsViewController

最佳答案

我不知道您在 prepareForSegue 中将图像传递到哪里,您是否将其加载到详细 View Controller 中?首先我会在detailVC中声明一个变量:

var image: UIImage?

然后我会在你的 ViewController 中传递图像:

   override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showDetails" {

let theDestination:DetailsViewController = segue.destinationViewController as! DetailsViewController
theDestination.toPass = (sender as! MKAnnotationView).annotation!.title!
theDestination.image = UIImage(named :"your-image-name")
}
}

然后在detailVC的viewDidLoad中,我将变量图像设置为我的IBOutlet ImageView:

override func viewDidLoad(){
self.imageView.image = self.image
}

关于ios - 将图像从 map 注释传递到详细信息 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36906028/

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