2016-07-07 2 views
0

私は、注釈を地図上に投稿できるアプリケーションを構築しています。注釈の詳細クロージャをクリックすると、画像とサブタイトル情報を含むViewControllerがロードされますが注釈情報を持つViewControllerをロードする方法

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 
} 
} 
私はdetailVCのviewDidLoadメソッド内の画像と字幕をロードしようとすると、それはここに私の注釈コードだ、それがnil

だと言います

詳細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) 
} 
} 

EDIT:

私は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 
} 
+0

prepareForSegueを実装していないようです:そこにDetailPingVCのpingプロパティを適切な値に設定します。 – Remover

+0

prepareForSegueはどこで実装できますか? – Doba

+0

UIViewControllerのドキュメントを見てください。オンラインの例もたくさんあるはずです。 – Remover

答えて

0

それを手に入れたnilを出てきます!その後、

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    let destViewController: DetailPingVC = segue.destinationViewController as! DetailPingVC 

    destViewController.descText = postTextField.text! 
    destViewController.detailImage = pickImage.image! 
} 

詳細VCののviewDidLoadに、あなたがアウトレットを設定:基本的に私がやったことはDetailVCに文字列型の変数と型UIImageの変数を追加して、私ののViewControllerの私prepareForSegueはように見えました以前に設定した変数に追加します。

関連する問題