Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 我可以使用willSet和/或didSet根据变量值触发到新视图控制器的转换吗?_Swift - Fatal编程技术网

Swift 我可以使用willSet和/或didSet根据变量值触发到新视图控制器的转换吗?

Swift 我可以使用willSet和/或didSet根据变量值触发到新视图控制器的转换吗?,swift,Swift,我是编程新手,今天我一直在研究Swift中的房地产观察家。这让我想知道,当变量的值达到某一点时,是否可以使用一个变量触发应用程序更改屏幕 例如,假设我有一个游戏,它使用变量“score”来保存和加载用户的分数。我可以使用willSet或didSet根据分数将达到某个值的事实触发视图的更改吗 我当时想的是这样的: var maxscore : Int = 0 { didSet{ if maxscore == 5{ switchScreen()

我是编程新手,今天我一直在研究Swift中的房地产观察家。这让我想知道,当变量的值达到某一点时,是否可以使用一个变量触发应用程序更改屏幕

例如,假设我有一个游戏,它使用变量“score”来保存和加载用户的分数。我可以使用willSet或didSet根据分数将达到某个值的事实触发视图的更改吗

我当时想的是这样的:

var maxscore : Int = 0 {
    didSet{
        if maxscore == 5{
        switchScreen()

        }}
}
。。。将调用开关屏幕功能。这样行吗?我还没有找到任何关于这方面的信息,所以不知道这是因为不可能,还是因为我没有找到它

但是我尝试过,但没有成功。它可以编译和运行,但是当分数达到5这个神奇的数字时,什么也没有发生

为完整起见,我的开关屏幕功能代码如下:

func switchScreen() {
    let mainStoryboard = UIStoryboard(name: "Storyboard", bundle: NSBundle.mainBundle())
    let vc : UIViewController = mainStoryboard.instantiateViewControllerWithIdentifier("HelpScreenViewController") as UIViewController
    self.presentViewController(vc, animated: true, completion: nil)
}
func CheckAnswer( answerNumber : Int)
{
    if(answerNumber == currentCorrectAnswerIndex)
    {
        // we have the correct answer
        labelFeedback.text = "Correct!"
        labelFeedback.textColor = UIColor.greenColor()
        score = score + 1
        labelScore.text = "Score: \(score)"

        totalquestionsasked = totalquestionsasked + 1
        labelTotalQuestionsAsked.text = "out of \(totalquestionsasked)"

        if score == 5 { maxscore = 5}
        // later we want to play a "correct" sound effect
        PlaySoundCorrect()
      }
    else
    {
        // we have the wrong answer
        labelFeedback.text = "Wrong!"
        labelFeedback.textColor = UIColor.blackColor()

        totalquestionsasked = totalquestionsasked + 1
        labelTotalQuestionsAsked.text = "out of \(totalquestionsasked)"

        if score == 5 { maxscore = 5}
        // we want to play a "incorrect" sound effect
        PlaySoundWrong()
    }

    SaveScore()
    buttonNext.enabled = true
    buttonNext.hidden = false
}
我用于将值设置为5的代码如下:

func switchScreen() {
    let mainStoryboard = UIStoryboard(name: "Storyboard", bundle: NSBundle.mainBundle())
    let vc : UIViewController = mainStoryboard.instantiateViewControllerWithIdentifier("HelpScreenViewController") as UIViewController
    self.presentViewController(vc, animated: true, completion: nil)
}
func CheckAnswer( answerNumber : Int)
{
    if(answerNumber == currentCorrectAnswerIndex)
    {
        // we have the correct answer
        labelFeedback.text = "Correct!"
        labelFeedback.textColor = UIColor.greenColor()
        score = score + 1
        labelScore.text = "Score: \(score)"

        totalquestionsasked = totalquestionsasked + 1
        labelTotalQuestionsAsked.text = "out of \(totalquestionsasked)"

        if score == 5 { maxscore = 5}
        // later we want to play a "correct" sound effect
        PlaySoundCorrect()
      }
    else
    {
        // we have the wrong answer
        labelFeedback.text = "Wrong!"
        labelFeedback.textColor = UIColor.blackColor()

        totalquestionsasked = totalquestionsasked + 1
        labelTotalQuestionsAsked.text = "out of \(totalquestionsasked)"

        if score == 5 { maxscore = 5}
        // we want to play a "incorrect" sound effect
        PlaySoundWrong()
    }

    SaveScore()
    buttonNext.enabled = true
    buttonNext.hidden = false
}

这是因为方法在类中,而不是您应该能够做到的!。检查答案

//True model data
var _test : Int = 0 {

//First this
willSet {
    println("Old value is \(_test), new value is \(newValue)")
}

//value is set

//Finaly this
  didSet {
     println("Old value is \(oldValue), new value is \(_test)")
  }
}

谢谢你的推荐。那里有很多信息-你认为我应该特别关注什么?