ios - 如何加载带有注释信息的 View Controller

标签 ios iphone swift mapkit

我正在构建一个应用程序,允许人们在 map 上发布注释,您可以单击注释的详细信息关闭,它会加载带有图像和字幕信息的 ViewController,但是当我尝试加载图像和DetailVC 的 viewDidLoad 方法中的副标题显示为 nil

这是我的注释代码:

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
}

最佳答案

明白了!基本上我所做的就是向 DetailVC 添加一个字符串类型的变量和一个 UIImage 类型的变量,然后我的 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 设置为之前设置的变量。

关于ios - 如何加载带有注释信息的 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38251002/

相关文章:

ios - 如果文件名中有空格,则 Deb 安装失败

iphone - 直接访问 Objective-C 中的嵌套字典值

ios - 使用 Parse 制作社交媒体应用程序或开发我自己的后端?

ios - AVCaptureSession 在 iPad 上工作正常但在 iPhone 上不行

swift - 如何搜索大型文本文件(书籍)?

objective-c - 平滑线 View 改变颜色

IOS:在没有 iphone/ipad 邮件应用程序的情况下发送邮件

ios - UIActivity 自定义分享,触摸更多按钮时不显示缩略图图标

ios - iTunes 通知服务器每次付款

ios - swift - 我可以连接到 socket.io 服务器,但我无法发出事件