如何加载带有注释信息的视图控制器



我正在构建一个应用程序,允许人们在地图上发布注释,您可以单击注释的细节闭合,它会加载带有图像和字幕信息的 ViewController 但是当我尝试在 detailVC 的 viewDidLoad 方法中加载图像和字幕时,它说它是零

这是我的注释代码:

extension ViewController: MKMapViewDelegate {
private struct Constants{
    static let identifier = "pin"
    static let LeftCalloutFrame = CGRect(x:0, y:0, width: 59, height: 59)
    static let ShowPinSegueIdentifier = "showDetail"
}
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
    if let annotation = annotation as? PingAnnotation {
        var view = mapView.dequeueReusableAnnotationViewWithIdentifier(Constants.identifier) as? MKPinAnnotationView
        if view == nil {
            view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: Constants.identifier)
            view?.canShowCallout = true
        } else {
            view!.annotation = annotation
        }
        view!.calloutOffset = CGPoint(x: -5, y: 5)
        view!.leftCalloutAccessoryView = UIImageView(frame: Constants.LeftCalloutFrame)
        view!.rightCalloutAccessoryView = UIButton(type: .DetailDisclosure) as UIView
        return view
    }
    return nil
}
func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
    if let thumbnailImageView = view.leftCalloutAccessoryView as? UIImageView {
        if let pingAnnotation = view.annotation as? PingAnnotation{
            thumbnailImageView.image = pingAnnotation.image
        }
    }
}
func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
    performSegueWithIdentifier(Constants.ShowPinSegueIdentifier, sender: view)
}

}

注释代码:

class PingAnnotation: NSObject, MKAnnotation {
var coordinate : CLLocationCoordinate2D
var title: String?
var subtitle: String?
var image : UIImage?
init(coordinate: CLLocationCoordinate2D, title: String?, subtitle: String?, image: UIImage?) {
    self.coordinate = coordinate
    self.title = title
    self.subtitle = subtitle
    self.image = image
}
}

详细VC代码:

import UIKit
class DetailPingVC: UIViewController {
var ping : PingAnnotation!

@IBOutlet var pingImage: UIImageView!
@IBOutlet var pingDesc: UILabel!
override func viewDidLoad() {
    super.viewDidLoad()
    pingImage.image = ping.image //shows as nil when ran
    pingDesc.text = ping.subtitle //shows as nil when ran

}

    @IBAction func pingBtnPressed(sender: AnyObject) {
}
    @IBAction func backBtnPressed(sender: AnyObject) {
    dismissViewControllerAnimated(true, completion: nil)
}
}

编辑:

我尝试使用prepareForSegue,但值仍然为零

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let destViewController: DetailPingVC = segue.destinationViewController as! DetailPingVC
    destViewController.pingImage.image = ping.image
    destViewController.pingDesc.text = ping.subtitle
}

知道了!基本上我所做的是将字符串类型的变量和UIImage类型的变量添加到DetailVC中,然后我的viewController中的prepareForSegue如下所示:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let destViewController: DetailPingVC = segue.destinationViewController as! DetailPingVC
    destViewController.descText = postTextField.text!
    destViewController.detailImage = pickImage.image!
}

然后在详细信息 VC 的 viewDidLoad 中,将插座设置为之前设置的变量。

最新更新