如何使用注释信息加载Viewcontroller

时间:2016-07-07 16:43:55

标签: ios iphone swift mapkit

我正在构建一个允许人们在地图上发布注释的应用程序,您可以单击注释的详细信息闭包,然后加载带有图像和字幕信息的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
}

1 个答案:

答案 0 :(得分:0)

知道了!基本上我所做的是将一个string类型的变量和一个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!
}

然后在Detail VC的viewDidLoad中将Outlets设置为您之前设置的变量。