Swift:NSTimer 自动结束而不倒计时 [英] Swift: NSTimer automatically ends without counting down

查看:56
本文介绍了Swift:NSTimer 自动结束而不倒计时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是 Swift 新手.我有两个问题:

Swift newbie here. I have two questions:

首先,如何创建一个在 ViewController 场景打开时自动倒计时的计时器?我的问题是 NSTimer 在场景打开时自动结束.

First, how do I create a timer which AUTOMATICALLY counts down when a ViewController scene is opened? My problem is that the NSTimer ENDS automatically when the scene is opened.

例如,每当我进入上述场景时,TimerLabel 都会说:时间到了!"

For instance, whenever I go to the said scene, the TimerLabel says: "Time’s Up!"

在我的第二个问题之前,下面是我调整后的代码:makeapppie.com/.../swift-swift-using-nstimer-to-make-a-timer.../

Before my second question, below is my tweaked code from: makeapppie.com/…/swift-swift-using-nstimer-to-make-a-timer…/

var timer = NSTimer()
let timeInterval:NSTimeInterval = 0.05
let timerEnd:NSTimeInterval = 120.0 //Timer should end in 120 seconds
var timeCount:NSTimeInterval = 0.0
func timerDidEnd(timer:NSTimer){
    timeCount = timeCount - timeInterval
    if timeCount <= 0 {
        TimerLabel.text = "Time's Up!"
        timer.invalidate()
    }
}

func timeString(time:NSTimeInterval) -> String {
    let minutes = Int(time) / 60
    let seconds = time - Double(minutes) * 60
    return String(format:"%02i:%02i",minutes,Int(seconds))
}

func StartTimer() { // Function called in viewDidLoad
    if !timer.valid{
        TimerLabel.text = timeString(timeCount)
        timer = NSTimer.scheduledTimerWithTimeInterval(timeInterval,
        target: self,
        selector: "timerDidEnd:",
        userInfo: nil,
        repeats: true)
    }
}

我的第二个问题是:我不想使用任何 UISwitch 来选择是让计时器倒计时还是倒计时.我只需要一个倒计时计时器,它会在打开视图时自动倒计时.我该怎么做?

My second question is this: I do not want to use any UISwitch to choose whether to have the timer counting up, or counting down. I need a COUNTING DOWN timer only which AUTOMATICALLY counts down when view is opened. How do I do that?

请注意,我的时间格式是:timeString 函数中描述的分:秒".

Please take note that my time format is: "minutes:seconds" as described in the timeString function.

请帮忙.

推荐答案

我的问题是 NSTimer 在场景打开时自动结束.

My problem is that the NSTimer ENDS automatically when the scene is opened.

这听起来像是您将 NSTimer 和管理它的代码放在视图控制器中.在这种情况下,当视图控制器消失时计时器消失也就不足为奇了.如果计时器应该从一个视图控制器持续到下一个,那么您应该从与视图控制器完全独立的对象中管理它.由于您不希望计时器对象必须跟踪所有可能对其感兴趣的视图控制器,因此让它发布感兴趣的视图控制器可以收听的通知.

It sounds like your putting your NSTimer and the code that manages it in a view controller. In that case, it's not surprising that the timer goes away when that view controller does. If the timer should persist from one view controller to the next, then you should manage it from an entirely separate object from the view controller. Since you don't want the timer object to have to keep track of all the view controllers that could possibly be interested in it, have it post notifications that interested view controllers can listen to.

这篇关于Swift:NSTimer 自动结束而不倒计时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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