在视图控制器之间传递数据产生 nil [英] Passing data between view controllers produces nil

查看:23
本文介绍了在视图控制器之间传递数据产生 nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名新程序员,正在努力在控制器之间传递数据,我知道在这些论坛中提出了这个问题,但与我的相比,上传的代码要复杂得多,我很难理解它,因此将上传我的几行代码我写了.我想允许两个玩家在我的应用程序中输入他们的名字,然后点击开始游戏.当按下开始游戏按钮时,它会将它们带到一个新的视图控制器,其名称类似于 Joe Bloggs vs John Doe,如果我将变量设为全局,我可以实现这一点,但我的理解是这是不好的做法,所以当我尝试时编写相同的代码,将变量保存在视图控制器中,它每次都传递 nil,但我不确定我做错了什么?这是我的几行代码,希望有人能回答我做错了什么?

I'm a new programmer and struggling with passing data between controllers, I am aware this question is asked in these forums but the uploaded code is much more complex compared to mine and i struggle to understand it so will upload my few lines i've written. I want to allow two players to type their names into my app and then click start game. when the start game button is pressed it takes them to a new view controller which has their names like this Joe Bloggs vs John Doe, I can achieve this IF i make my variables global but my understanding is this is bad practice so when i try and write the same code keeping the variables inside the View Controller it passes nil each time and i'm unsure what i have done wrong? Here is my few lines of code that i hope someone can answer what i'm doing wrong?

视图控制器:

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

}

游戏视图控制器:

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.

    }

}

推荐答案

您不能在控制器初始化之前设置 @IBOutlets 的属性.您需要在第二个视图控制器中存储诸如 String! 之类的值.例如,在 viewDidLoad 方法中使用它们初始化 @IBOutlets.

You can't set properties of @IBOutlets before you controller is initialized. You need to store values like String! in second view controller. And init @IBOutlets with them in viewDidLoad method for example.

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.
    }
}

ViewController中的prepare:

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

希望能帮到你

这篇关于在视图控制器之间传递数据产生 nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆