代码之家  ›  专栏  ›  技术社区  ›  aleksy.t

无法通过segue传递数据。敏捷的

  •  -3
  • aleksy.t  · 技术社区  · 7 年前

    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()
    }
    

    背包4ViewController:

    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 回复  |  直到 7 年前
        1
  •  2
  •   matt    7 年前

    问题是,这不是一个将被调用的方法:

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

    这是毫无意义的;它不会被调用,您的代码也永远不会运行。

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

    现在将调用您的方法。