无法通过segue传递数据。迅速

时间:2017-09-14 20:01:16

标签: ios swift memory segue

我无法通过ViewControllers传递变量,即使我老实说我已经完成了所有必需品。

Game3ViewController:

class Game3ViewController: UIViewController, CLLocationManagerDelegate
{
var START = 1

var zombieMarkersCenters: [[Double]] = Array(repeating:Array(repeating:0, count:2), count:10)

var zombieHealth: [Int] = Array(repeating:100, count:10)

var HP = 100

var bullets = [0,0]

/* ... REST OF THE CODE */

override func viewDidAppear(_ animated: Bool)
{
/* here I assign values to the variables */
}

/* ... REST OF THE CODE */

func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
    if segue.identifier == "goToBackpack"
    {
        let Backpack4 = segue.destination as! Backpack4ViewController

        Backpack4.zombieMarkersCentersBackpack = zombieMarkersCenters
        Backpack4.bulletsBackpack = bullets
        Backpack4.STARTBackpack = START
        Backpack4.zombieHealthBackpack = zombieHealth
    }
}

/* ACTIONS */

@IBAction func openBackpack(_ sender: Any)
{   
    performSegue(withIdentifier: "goToBackpack", sender: self)

    timer.invalidate()
}

Backpack4ViewController:

class Backpack4ViewController: UIViewController
{
    var zombieMarkersCentersBackpack = [[Double]]()
    var bulletsBackpack = [Int]()
    var STARTBackpack = Int()
    var zombieHealthBackpack = [Int]()

override func viewDidLoad()
    {
        super.viewDidLoad()
        print("WORKS?: ", bulletsBackpack, STARTBackpack, zombieHealthBackpack)

    /* ... REST OF THE CODE */

打印结果为:[] 0 [],所以我猜数据通过从未发生过。我该怎么办?

1 个答案:

答案 0 :(得分:2)

问题是这不是一个永远被调用的方法:

func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

那毫无意义;它不会被调用,你的代码永远不会运行。

你需要写下这个:

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

现在将调用您的方法。