ios - 如何设置NSTimer的计数限制?

标签 ios swift

如何设置 NSTimer 计数限制?

首先,为了了解我的问题,这是我的 Xcode 项目的屏幕截图 - http://i.gyazo.com/178450cfdbf45a2508856cc79fd215ec.png

其次,这是了解问题所在的代码 -

    else if timerRunning==true && timerCount==timerMaximum {
        timer.invalidate()
        timerRunning = false
        timerCount=0
        timerLabel.text="0 secs"
        intervalAlert()
    }

我尝试这样做,代码对程序的执行没有影响。计时器在timerMaximum变量(我确保它是全局的)之后继续计数。

我认为这与...有关

if timerRunning==false{
        timer=NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("Counting"), userInfo: nil, repeats: true)
        timerRunning=true
    }

不过,我不太确定。

有人可以帮助我吗?

非常感谢!

杰米

最佳答案

counting方法中检查和限制timerCount。喜欢:

func Counting() {

    timerCount+=1

    if timerCount == timeraximum {

        self.stop()
    }
    else {

        timerLabel.text =“\(timerCount) seconds”
    }

关于ios - 如何设置NSTimer的计数限制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31721379/

相关文章:

ios - 如何按小时对 HKStatistics 进行分组?

ios - 无论语言环境如何,如何强制 Swift Decimal Pad 使用点而不是逗号?

ios - CKReference 的 CloudKit Delete Self 选项不起作用

ios - 文本在 iOS swift 的 uitableview 单元格中重叠

ios - 上传图像解析ios 9

swift - 是否可以访问通过 Apple Music 下载的音乐?

ios - 无法在标签中显示字典对象值

ios - ASIHTTPRequest 在生产构建中失败

iphone - Xcode 4.1 问题

ios - Swift:cellForRowAtIndexPath 中出现意外结果