我尝试使用许多其他策略,但到目前为止还没有成功。

我做过一个真假游戏。如果您得到正确的答案,您的分数将增加一分。如果弄错了,您将被发送到GameOver View Controller。

我正在尝试做的就是获取您获得的分数,从SecondVC转移到GameOverVC。

var Score = 0

if AnswerNumber == 0 {
    Score += 1
    Score_Keeper.text = NSString(format:"%i",Score) as String


(Score_Keeper是我的标签名称,它向您显示您在SecondVC上的当前分数)

else {

     let destinationController = storyboard?.instantiateViewControllerWithIdentifier("ThirdVC")
     presentViewController(destinationController!, animated: true, completion: nil)


这将我带到我的GameOverVC。要在GameOver屏幕中为我的UILabel分配“分数”,我已经在代码的最后写了。

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let SecondVC: GameOver = segue.destinationViewController as! GameOver

    SecondVC.LabelText == Score


我的GameOverVC中的LabelText是

var LabelText = Int()


在我的“查看是否加载”中,我写道:

Score_Number.text = "\(LabelText)"


其中Score_Number是我的UILabel的名称。基本上,我希望GameOverVC中的Score_Number = Second VC中的Score。

我希望我的问题很清楚...非常感谢!

最佳答案

您不需要实现prepareForSegue。只需在显示值之前分配值

 let destinationController = storyboard?.instantiateViewControllerWithIdentifier("ThirdVC") as GameOverVC
 destinationController.LabelText = score;
 presentViewController(desinationController!, animated: true, completion: nil)


同样在您的prepareforSegue作业中

SecondVC.LabelText = Score


代替

SecondVC.LabelText == Score

关于ios - 将数据从1个 View Controller 传递到另一个,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36528442/

10-10 03:57