如何在Swift中的类之间正确发送值?

时间:2019-04-18 04:04:06

标签: swift segue var

我一直在使用Swift开发基于文本的冒险游戏。但是,我似乎无法更改特定类的默认值。

下面是该课程的代码,允许我选择播放器课程

    import UIKit

    class ClassSelectionController: UIViewController
    {
        //Default class values
        var character = (0, 0, "", 0)

        override func viewDidLoad()
        {
                super.viewDidLoad()
                // Do any additional setup after loading the view.
        }

        //class button actions
        @IBAction func fighterBtn(_ sender: Any)
        {
                character = (50, 60, "Steal Sword", 18)
                performSegue(withIdentifier: "Character", sender: self)
        }

        @IBAction func wizerdBtn(_ sender: Any)
        {
                character = (25, 70, "Staff", 15)
                performSegue(withIdentifier: "Character", sender: self)
        }

        @IBAction func thiefBtn(_ sender: Any)
        {
                character = (30, 60, "Dagger", 18)
                performSegue(withIdentifier: "Character", sender: self)
        }

        @IBAction func archerBtn(_ sender: Any)
        {
                character = (50, 60, "Bow & Arrow", 16)
                performSegue(withIdentifier: "Character", sender: self)
        }


        override func prepare(for segue: UIStoryboardSegue, sender: (Any)?)
        {
                //code for segue
                var vc = segue.destination as! ViewController
                vc.finalCharacter = self.character
        }

    }

这是接收播放器类数据并显示它的类。

    import UIKit

    class ViewController: UIViewController
    {
            var finalCharacter = (0, 0, "", 0)
            //****************************************
            //Setup for outlets go between these lines
            //****************************************
            @IBOutlet weak var healthLabel: UILabel!
            @IBOutlet weak var damageLabel: UILabel!
            @IBOutlet weak var weaponLabel: UILabel!
            @IBOutlet weak var armorLabel: UILabel!
            @IBOutlet weak var storyLabel: UILabel!
            @IBOutlet weak var actionButton: UIButton!


            @IBOutlet weak var northBtn: UIButton!
            @IBOutlet weak var southBtn: UIButton!
            @IBOutlet weak var eastBtn: UIButton!
            @IBOutlet weak var westBtn: UIButton!

            //****************************************
            //Setup for outlets go between these lines
            //****************************************


            override func viewDidLoad()
            {
                    super.viewDidLoad()
                    // Do any additional setup after loading the view.

                    healthLabel.text = "Health: \(finalCharacter.0)"
                    damageLabel.text = "Damage: \(finalCharacter.1)"
                    weaponLabel.text = "Weapon: " + finalCharacter.2
                    armorLabel.text  = "Armor : \(finalCharacter.3)"
            }

            //****************************************
            //Setup for buttons go between these lines
            //****************************************
            @IBAction func actionButton(_ sender: Any)
            {

            }

            @IBAction func northButton(_ sender: Any)
            {

            }

            @IBAction func southButton(_ sender: Any)
            {

            }

            @IBAction func eastButton(_ sender: Any)
            {

            }

            @IBAction func westButton(_ sender: Any)
            {

            }
    //****************************************
    //Setup for buttons go between these lines
    //****************************************
    }

即使我的代码当前未显示它,我也确实在类按钮中放置了打印值,以便查看选择类时这些值是否已更改,并且我发现按下按钮时它们确实发生了更改。我还查看了ViewController并更改了finalCharacter值,以查看是否影响了任何东西,但没有影响。因此,我有根据的猜测是问题必须出在ClassSelectionController中。

因此,主要问题是我将单击向导播放器类,并且希望弹出该向导的类统计信息(即25、70,“ Staff”,15),但我只会获取ClassSelectionController中的默认值(0、0,“”,0)。

1 个答案:

答案 0 :(得分:2)

这是怎么回事,您有多个与每个按钮关联的名为“字符”的NuGet.Config

所以发生的事情是,当您按下按钮时,Segue在“动作”按钮之前被调用,另外UIStoryboardSegue被再次调用(如果将调试器放在UIStoryboardSegue中,您会看到它去那里两次)。

解决方案

  • 删除所有从按钮链接的viewDidLoad
  • UIStoryboardSegue到下一个UIStoryboardSegue新建一个ClassSelectionController,并将其命名为“字符” see here

您无需更改代码中的任何内容,但应添加安全检查

ViewController