检查用户是否导航到iOS中的另一个视图



我有一个简单的游戏,当游戏开始时,它会启动一个计时器。该应用程序已安装好导航控制器。问题是,用户可以导航离开游戏,计时器将在应用程序的后台继续倒计时。

有没有办法检查是否执行了分段,这样我就可以停止游戏和计时器?这是我目前用来倒计时的功能:

func Counting(){
    if timerCount == 0 // OR if user navigates away from game
    {
        timerCount = 7
        self.timer.invalidate()
        self.timerRunning = false
        endTurn()
    }
    else
    {
        self.timerRunning = true
        timerCount--
        timerLabel.text = "(timerCount)"
        print(timerCount)
    }
 }

谢谢!

简单!你想到的是prepareForSegue方法!每当你执行segue时,这个方法就会在你离开屏幕之前被调用。以下是如何将其与代码集成。

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
   self.timerRunning = false

    // If you need do to logic with your destination VC before load
    let destinationVC = segue.destinationViewController as destinationVCType
}

最新更新