2016-10-17 8 views
0

あるVC上のアレイにアイテムを追加し、アレイを別のVCに転送するために「segueの準備」を使用する最良の方法は何ですか? (VC1)アレイでSegueを準備する - Xcode 8.0 Swift 3.0

var items: [String] = ["Hello"] 

(VC2):

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

    var destViewController: ViewController = segue.destination as! ViewController 
    destViewController.items = [textField.text!] 
    items.append(textField.text!) 
} 

がVC2でエラーが来ているという状態、「未解決の識別子を使用し、」これまでのところ、これは私が思い付くに管理しているものです回線上で

items.append(textField.text!) 
+0

destViewController.items.append(textField.text!)それを直接使用 – zombie

+0

私は 'destViewController.items =削除する必要があります[textField.textを!]'と 'と交換destViewController.items.append(textField.text! ' –

+0

2つの最後の行の両方 – zombie

答えて

3

私はiOS/Swiftにはかなり新しいですが、私は最近同じ状況に遭遇しました。ここで私はそれをやっている。

SourceViewController.swift

class SourceViewController: UIViewController { 
    let stringToPass = "Hello World" 

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
     let destinationVC = segue.destination as! DestinationViewController 
     destinationVC.receivedString = stringToPass 
     } 
    } 

DestinationViewController.swift

class DestinationViewController: UIViewController { 

    var receivedString: String? 

    if let newString = receivedString { 
     print(newString) 
    } 
... 

私は、これはあなたの例よりもわずかに異なっている実感が、注意すべき重要なことは、あなたが「destinationVC」を作成するとき、あなたがしていることですそのプロパティを変更することができます。 (スコープのXcodeを設けることなく、

destViewController.items.append(textField.text!) 

変数を見つけることができません:主な違いは、あなたが値を割り当てるか、あなたのケースでは、アレイに追加する際に、変数(destinationVC.receivedString)の範囲を提供する必要があります現在のファイルの一部またはインポートの一部ではないため、変更しようとしています)。

+0

どのように配列にアイテムを追加するのですか? –

+0

あなたはあなたがどのように表示したか正確にそれを行うでしょう: 'destViewController.items.append(textField.text!)' – lostinswift

0

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
 
print("thank you very much for your helpful suggestions") 
 
print("I think I am pointed at the right direction") 
 
print("Thanks again for your generous contributions") 
 
print("Apoligies! Stackoverflow is pretty well automated to allow me to put it in non programatic format") 
 
}

+0

print()関数* –

関連する問題