在视图控制器之间传递数据会产生nil

时间:2017-06-21 12:30:55

标签: ios swift xcode viewcontroller

我是新程序员,在控制器之间传递数据时苦苦挣扎,我知道这些问题在这些论坛中被提出但是上传的代码比我的复杂得多,我很难理解它,所以会上传我的几个我已经写好了。我想允许两个玩家在我的应用中输入他们的名字,然后点击开始游戏。当按下开始游戏按钮时,它会将它们带到一个新的视图控制器,它的名字就像这个Joe Bloggs vs John Doe,我可以实现这个,如果我让我的变量全局但我的理解是这是不好的做法,所以当我尝试和写相同的代码保持视图控制器内的变量每次都传递为零,我不确定我做错了什么?这是我的几行代码,我希望有人可以回答我做错了什么?

的ViewController:

import UIKit

class ViewController: UIViewController {

    var playerOneName: String!
    var playerTwoName: String!

    @IBOutlet weak var playerOneTextField: UITextField!
    @IBOutlet weak var playerTwoTextField: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    @IBAction func startGameButton(_ sender: Any) {

        playerOneName = playerOneTextField.text!
        playerTwoName = playerTwoTextField.text!

    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "segue" {

            let destinationVC = segue.destination as! GameViewController
            destinationVC.playerOne.text = playerOneName
            destinationVC.playerTwo.text = playerTwoName
        }
    }

}

GameViewController:

import UIKit

class GameViewController: UIViewController {

    @IBOutlet weak var playerOne: UILabel!
    @IBOutlet weak var playerTwo: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

    }

}

3 个答案:

答案 0 :(得分:2)

在初始化控制器之前,您无法设置@IBOutlets的属性。 您需要在第二个视图控制器中存储String!之类的值。并以@IBOutlets方法为例初始化viewDidLoad

GameViewController代码更改为:

import UIKit

class GameViewController: UIViewController {
    @IBOutlet weak var playerOne: UILabel!
    var playerOneName: String!     

    @IBOutlet weak var playerTwo: UILabel!
    var playerTwoName: String!

    override func viewDidLoad() {
        super.viewDidLoad()

        playerOne.text = playerOneName
        playerTwo.text = playerTextName
        // Do any additional setup after loading the view.
    }
}

prepare中的ViewController

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "segue" {
        let destinationVC = segue.destination as! GameViewController
        destinationVC.playerOneName = playerOneName
        destinationVC.playerTwoName = playerTwoName
    }
}

希望有所帮助

答案 1 :(得分:1)

您不应直接从另一个视图控制器设置标签的文本,而是在GameViewController中创建变量以存储标签文本,在segue中更改这些变量,然后更改目标控制器中的标签{{1 }} 方法。见下面的代码。

viewDidLoad

在ViewController中:

class GameViewController: UIViewController {

    @IBOutlet weak var playerOne: UILabel!
    @IBOutlet weak var playerTwo: UILabel!

    var playerOneName:String?
    var playerTwoName:String?

    override func viewDidLoad() {
        super.viewDidLoad()
        playerOne.text = playerOneName
        playerTwo.text = playerTwoName
        // Do any additional setup after loading the view.

    }

}

答案 2 :(得分:0)

在您的控制器视图加载之前,您的网点无法初始化。然而,在此之前就发生了这种情况。

最好的解决方案是将一些字符串属性放入GameViewController并使用它们从第一个控制器接收数据。然后,在viewDidLoad更新标签文字。