2016-11-23 15 views
0

私はIOS9アプリケーション開発エッセンシャルというタイトルの本を読んでIOS開発を勉強しています。Segueでデータを渡すことができません - Swift3

私は 'segue partを準備しようとすると'エラーがあります。私はコードがswift3から変更されたと思う、私はどのようにそれを把握するのか分からない。

コードが

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    let destination = segue.destinationViewController as! 
    Scene2ViewControllerdestination.labelText = "arrived from scene1" 
} 
+0

は、エラーがどのように見えるかを追加してください! –

答えて

0

に従っているあなたのコードは動作しません、あなたはセグエ識別子(ベストプラクティス)
2をご確認ください)Scene2ViewControllerdestination

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 

    let destination = segue.destinationViewController as! Scene2ViewControllerdestination 
    destination.labelText = "arrived from scene1" 
} 
0

1のインスタンスを使用する必要があります)を作成宛先ビューコントローラのオブジェクト
3)オブジェクトを使用して必要なデータを渡す

あなたは正しいセグエの識別子を使用している場合、210

はあなたの参照

override func prepare(for segue: UIStoryboardSegue, sender: Any?) 
    { 

     if segue.identifier == YOUR_SEGUE_IDENTIFIER 
     { 
      let objSummery : SummeryVC = segue.destination as! SummeryVC 
      objSummery.strUserID = YOUR_VALUE 
     } 

    } 
0
  1. チェック用のコードの下に参照してください。 Segueの識別子はMain.storyboardファイルにあります。 enter image description here
  2. あなたのviewControllerがUINavigationViewControllerに埋め込まれているかどうかを確認してください。それがない場合は、その後、このことができます。この

    override func prepare(for segue: UIStoryboardSegue, sender: Any?){ 
    if segue.identifier == "your_identifier" { 
    let destinationVC = segue.destinationViewController as! Scene2ViewControllerdestination 
    destinationVC.labelText = "arrived from scene1" 
    } 
    

    希望のようなものを使用している場合

は、この

override func prepare(for segue: UIStoryboardSegue, sender: Any?){ 
if segue.identifier == "your_identifier" { 
     let destinationVC = segue.destinationViewController as! UINavigationViewController 
     let sourceVC = destinationVC.viewControllers[0] as! Scene2ViewControllerdestination 
     sourceVC.labelText = "arrived from scene1" 
    } 
} 

ようなものを使用します。 :)

0

コードスニペットにはいくつかの問題があります。

  • それは文法的に正しくないです(と私はScene2ViewControllerdestinationの間、ちょうど改行欠けがあることに気づいたこの答えを書き込んだ後、当たり前)。
  • destinationViewControllerの名前がdestinationに変更されました。
  • Milap Kandaliaが示唆するように、正しいidentifierをチェックしてください(これは複数の送信セグがある場合に重要になります)。 bhakti123が示すように、segueの識別子を設定する必要があることに注意してください。

すべて一緒にそれを置く、これは動作するはずです:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    if segue.identifier == "MeaningfulIdentifier", 
     let destination = segue.destination as? Scene2ViewController 
    { 
     destination.labelText = "arrived from scene1" 
    } 
} 
関連する問題