简体   繁体   English

如何在Swift中的两个场景之间传递数据?

[英]How do I pass data between two scenes in Swift?

I have two scenes where I would like to pass a single variable to another scene using a segue. 我有两个场景,我想使用segue将单个变量传递给另一个场景。 I have tried but unfortunately all tutorials I have seen are dealing with the storyboard. 我已经尝试过,但是不幸的是,我所看到的所有教程都与情节提要有关。 I am not using the storyboard. 我没有使用情节提要。 I am doing all of this programatically. 我正在以编程方式进行所有这些操作。

Here is the segue i am trying to initialize: 这是我要初始化的segue:

func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
    if (segue.identifier == "segueTest") {
        var lose = segue.destinationViewController as! loserScene;

        lose.toPass = scoreLabelNode.text

    }
}

The Loser Scene is my second scene. 失败者场景是我的第二个场景。
scoreLabelNode.text is the text of an NSTimer which i'm using as a score. scoreLabelNode.text是我用作分数的NSTimer的文本。

I want to move the scoreLabelNode.text into another scene which is my loserScene. 我想将scoreLabelNode.text移动到另一个场景,即我的LoserScene。

My loserScene is set up like this: 我的loserScene设置如下:

    import SpriteKit
import Foundation

let GameOverLabelCategoryName = "gameOverLabel"

class loserScene: SKScene {
    var toPass: String!
    var scoreLabel = SKLabelNode(fontNamed:"[z] Arista Light")



    override func didMoveToView(view: SKView) {
        var gameOver = SKSpriteNode(imageNamed: "gameover.png")
        gameOver.position = CGPoint(x: self.frame.size.width/2, y: self.frame.size.height/2)
        self.addChild(gameOver)
        println(toPass)




    }
}

I am trying to print 'toPass' to test the string segue, but I just get nil. 我正在尝试打印“ toPass”以测试字符串segue,但是我得到的结果为零。

OK! 好! So instead of using a segue. 因此,不要使用segue。 WHICH YOU CANNOT DO WITH AN SKSCENE. 您无法使用SKSCENE。 I used a struct. 我使用了一个结构。 I put the struct outside of one of my scenes that I wanted data to be passed from, and made sure the data was being fed into the struct. 我将结构放置在我想要传递数据的场景之一之外,并确保将数据馈入结构中。 I then accessed the struct from another scene and it works perfectly! 然后,我从另一个场景访问了该结构,它运行完美!

struct Variables {
static var aVariable = 0
}

this is the struct^ I have it set to 0 but it will be updated automatically when the score is recorded at the end of the run of my game. 这是struct ^,我将其设置为0,但是当在游戏结束时记录得分时,它将自动更新。 I then accessed it like so in another scene: 然后在另一个场景中像这样访问它:

print(Variables.aVariable)

This prints the variable from the struct in my new scene. 这将在我的新场景中从结构中打印变量。 I can also turn this struct into a string and use it for a labelnode. 我也可以将此结构转换为字符串并将其用于labelnode。

Your toPass String returns nil because it is not initialized when you send the value from your prepareForSegue method. 您的toPass字符串返回nil,因为当您从prepareForSegue方法发送值时,该字符串未初始化

Create a some function for example; 例如创建一些函数;

// Do whatever you need with the toPass variable.
func printToPassValue(){
     print(toPass)
}

And add the property observers to your toPass variable. 并将属性观察器添加到您的toPass变量。 like this; 像这样;

var toPass: String? {
    willSet {
     printToPassValue() // Call your function
     }
}

And add this function to your viewDidLoad method. 并将此函数添加到您的viewDidLoad方法。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM