加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

在Swift中使用NSTimer

发布时间:2020-12-14 06:04:53 所属栏目:百科 来源:网络整理
导读:在这种情况下,timerFunc()从未被调用。我缺少什么? class AppDelegate: NSObject,NSApplicationDelegate { var myTimer: NSTimer? = nil func timerFunc() { println("timerFunc()") } func applicationDidFinishLaunching(aNotification: NSNotification?
在这种情况下,timerFunc()从未被调用。我缺少什么?
class AppDelegate: NSObject,NSApplicationDelegate {

    var myTimer: NSTimer? = nil

    func timerFunc() {
        println("timerFunc()")
    }

    func applicationDidFinishLaunching(aNotification: NSNotification?) {
        myTimer = NSTimer(timeInterval: 5.0,target: self,selector:"timerFunc",userInfo: nil,repeats: true)
    }
}
您可以创建一个自动将自己添加到runloop并开始启动的计划定时器:
NSTimer.scheduledTimerWithTimeInterval(0.5,selector: "timerDidFire:",userInfo: userInfo,repeats: true)

或者,你可以保留你当前的代码,并添加定时器runloop当你准备好了:

let myTimer = NSTimer(timeInterval: 0.5,repeats: true)
NSRunLoop.currentRunLoop().addTimer(myTimer,forMode: NSRunLoopCommonModes)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读