NSTimer timerWithTimeInterval 不调用选择器(Swift) [英] NSTimer timerWithTimeInterval not calling selector (Swift)

查看:41
本文介绍了NSTimer timerWithTimeInterval 不调用选择器(Swift)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了许多在其他问题中发现的方法,其中任何一种都有效.我的问题是计时器没有调用选择器

I have tried many approaches that I have found in other questions and any of them are working. My problem is that the timer is not calling selector

class MyViewController: UIViewController{
    var progressBarTimer = NSTimer()
    @IBOutlet weak var progress_bar: UIProgressView!

    override func viewDidLoad() {
        self.progress_bar.progress = 0
        self.progressBarTimer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(MyViewController.updateProgress(_:)), userInfo: nil, repeats: true)
        //also tried and redefining the updateProgress without timer parameter:
        /*
        *self.progressBarTimer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(MyViewController.updateProgress), userInfo: nil, repeats: true)
        */
    }
    func updateProgress(timer:NSTimer!) {
        progress_bar.progress += 0.1
        if (progress_bar.progress >= 1) {
           progressBarTimer.invalidate()
        }

    }
}

我已经尝试过

progressBarTimer.fire()

它只执行一次 updateProgress 函数.

and it just executes once the updateProgress function.

有人能解释一下吗?我真的很感激

Could anyone shed light on? I would really appreciate

推荐答案

回复自己,发现问题了.在之前的 ViewController 中,我在进行 http 连接后执行 segue.为了使它工作,我在调度中嵌入了 performSegue:

Replying to myself, I have found the problem. In the previous ViewController I am performing a segue after doing a http connection. In order to make it work, I have embedded the performSegue inside the dispatch:

dispatch_async(dispatch_get_main_queue(), {
    self.performSegueWithIdentifier("goMySecondController", sender: nil)
})

在第二个控制器中,我的计时器无法正常工作.但是,在此更改后正常工作.

In that second Controller is where I have the Timer that was not working. However, after this change is working properly.

感谢回复!

这篇关于NSTimer timerWithTimeInterval 不调用选择器(Swift)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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