如何在视图控制器之间传递数据-不起作用 [英] How to pass data between view controllers - not working

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

问题描述

我正在尝试将玩家得分从VC1传递到显示所有玩家当前得分的视图(4).显示视图位于与定义和存储玩家得分无关的单独视图控制器上.

Im trying to pass an player score from VC1 to a view that displays the current scores of all players(4). the display view is on a separate view controller than where the player(s) score is defined and stored.

我已经完成了prepare(segue),并且无法将其他变量传递给显示VC(ThirdViewController).但是当我尝试将玩家得分分配给uilabel.text时,它告诉我我解开了一个nil值

I have done the prepare(segue) and im able to pass other variables to the display VC(ThirdViewController). but when I try to assign the player score to the uilabel.text It tells me that I have unwrapped a nil value

我什至试图将标签文本设置为静态字符串,但仍然出现nil错误.

I have even tried to just set the label text to a static string and still get the nil error.

class ViewController: UIViewController {

var name = String()

var player1Score = 1
var player2Score = 2
var player3Score = 3
var player4Score = 4

//MARK: ********* IBOutlets **********
@IBOutlet weak var playerSegmentOutlet: UISegmentedControl!
@IBOutlet weak var diceSegmentOutlet: UISegmentedControl!
@IBOutlet weak var targetScoreSliderOutlet: UISlider!
@IBOutlet weak var matchTargetSwitchOutlet: UISwitch!
@IBOutlet weak var targetScoreLabel: UILabel!


override func viewDidLoad() {
    super.viewDidLoad()

}


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

    if let VC = segue.destination as? SecondViewController {

        VC.player1CurrentScore = player1Score
        VC.player2CurrentScore = player2Score
        VC.player3CurrentScore = player3Score
        VC.player4CurrentScore = player4Score
    }
}

第二个视图控制器

class SecondViewController: UIViewController {

@IBOutlet weak var CurrentRoundScoreLabel: UILabel!

@IBOutlet weak var CurrentPlayerScoreLabel: UILabel!

var player1CurrentScore = 1
var player2CurrentScore = 1
var player3CurrentScore = 1
var player4CurrentScore = 1


override func viewDidLoad() {
    super.viewDidLoad()

}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let VC = segue.destination as? ThirdViewController {

        VC.player1ScoreLabel.text = String(player1CurrentScore)
        VC.player2ScoreLabel.text = String(player2CurrentScore)
        VC.player3ScoreLabel.text = String(player3CurrentScore)
        VC.player4ScoreLabel.text = String(player4CurrentScore)

    }
}

第三视图控制器

class ThirdViewController: UIViewController {

@IBOutlet var player1ScoreLabel: UILabel!
@IBOutlet var player2ScoreLabel: UILabel!
@IBOutlet var player3ScoreLabel: UILabel!
@IBOutlet var player4ScoreLabel: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()
}

}

无论我尝试使用UILabel.text做什么,它都显示为nil

no matter what I try to do with with UILabel.text it shows up as nil

我完全感到沮丧,我确信由于沮丧而错过了一些简单的事情,请有人帮助我.

I'm totally frustrated and I'm sure I am missing something simple because of my frustration, please someone help me.

推荐答案

在通过3个不同的对象传递数据时,这是一种低效的方法.但是,继续使用此方法,问题是标签尚未在

This is an inefficient way to do this as you are passing data through 3 different objects. However, going on with this methodology, the problem is the label's are not created yet inside

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let VC = segue.destination as? ThirdViewController {

        VC.player1ScoreLabel.text = String(player1CurrentScore)
        VC.player2ScoreLabel.text = String(player2CurrentScore)
        VC.player3ScoreLabel.text = String(player3CurrentScore)
        VC.player4ScoreLabel.text = String(player4CurrentScore)

    }
}

请参见,标签尚未创建.因此,您要在未初始化的UILabel上设置text.因此,您需要为ThirdViewController中的标签创建变量.

See, the label isn't created yet. So, you are setting text on aUILabel that isn't initialized. Therefore, you need to create variables for the labels inside ThirdViewController.

第三视图控制器

class ThirdViewController: UIViewController {

    @IBOutlet var player1ScoreLabel: UILabel!
    @IBOutlet var player2ScoreLabel: UILabel!
    @IBOutlet var player3ScoreLabel: UILabel!
    @IBOutlet var player4ScoreLabel: UILabel!

    var score0:Int!
    var score1:Int!
    var score2:Int!
    var score3:Int!

    override func viewDidLoad() {
        super.viewDidLoad()
        self.player1ScoreLabel.text = String(score0)
        self.player2ScoreLabel.text = String(score1)
        self.player3ScoreLabel.text = String(score2)
        self.player4ScoreLabel.text = String(score3)
    }
}

并在SecondViewController中更改序号

and change the segue in SecondViewController

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let VC = segue.destination as? ThirdViewController {

        VC.score0 = player1CurrentScore
        VC.score1 = player2CurrentScore
        VC.score2 = player3CurrentScore
        VC.score3 = player4CurrentScore

    }
}


另一种方式:

让我们创建一个称为Game的单例.在此范围内(假设您只有4个玩家),我们可以创建4个不变的玩家.这使我们可以在一个位置创建玩家实例,并在必要时调用它们.

Let's create a singleton called Game. Within this (assuming you only have 4 players) we can create 4 players that will never change. This allows us to create instances of players in one location and call upon them as necessary.

注意::单例很容易被滥用.

NOTE: A singleton can be misused EASILY.

https://cocoacasts .com/singleton是什么,以及如何快速创建一个 https://cocoacasts.com/are-singletons-bad/

class Game {

    static var score0:Int = 0
    static var score1:Int = 0
    static var score2:Int = 0
    static var score2:Int = 0

}        

然后,在代码中的任何地方都可以访问Game.score0,Game.score1.

Then, anywhere in your code you can access Game.score0, Game.score1.

注意:

我会提醒您非常小心地使用单例.您不希望所有内容都具有公共访问权限.您需要确定这是否对您有好处.干杯.

I would caution you to very carefully use singletons. You don't want everything with public access. You need to determine if this is good for you or not. Cheers.

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

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