NSTimer没有解雇

时间:2016-05-11 18:16:29

标签: ios swift nstimer

我在viewDidLoad()时为firer设置了一个计时器类。我希望在应用程序的多个视图控制器上有一个计时器。如果你有更好的解决方案在多个视图上的准确计时器,请建议。

Viewcontroller - >其中一个需要计时器的视图

 override func viewDidLoad() {
 super.viewDidLoad()

    func setupTimer() {

        // Setupt the timer, this will call the timerFired method every second
        var timer = NSTimer(
            timeInterval: 1.0,
            target: self,
            selector: #selector(TestTimer.timerFired()),//<- Error Code
            userInfo: nil,
            repeats: true)
         }

错误代码:在'TestTimer'类型上使用实例成员'timerFired',你的意思是使用'TestTimer'类型的值吗?

计时器类 - &gt;检查开始日期与当前日期/时间相比的准确计时器

 class TestTimer: NSTimer {

var timer   = NSTimer()
// Converter changes String into NSDate
var startDate = converter("Tue, 26 Apr 2016 09:01:00 MDT")
// Function to be fired
func timerFired() {

    let now = NSDate()
    let difference = now.timeIntervalSinceDate(self.startDate)


    // Format the difference for display
    // For example, minutes & seconds
    let dateComponentsFormatter = NSDateComponentsFormatter()
    dateComponentsFormatter.stringFromTimeInterval(difference)
    print(difference)

 }
 }

2 个答案:

答案 0 :(得分:1)

你得到的错误非常模糊。它试图告诉您的是,您应该从()timerFired的末尾删除#selector

var timer = NSTimer(
    timeInterval: 1.0,
    target: self,
    selector: #selector(TestTimer.timerFired),
    userInfo: nil,
    repeats: true)

但是,这不会使您的代码如何工作 - 因为计时器声明中的self是指视图控制器,而不是计时器。我建议你为NSTimer创建一个包装类,以及一个委托模式,以实现你想要的。

您应该注意the documentation states您不应该尝试继承NSTimer,所以您可以这样做:

// the protocol that defines the timerDidFire callback method
protocol TimerDelegate:class {
    func timerDidFire(cumulativeTime:NSTimeInterval)
}

// your timer wrapper class
class TimerWrapper {

    // the underlying timer object
    weak private var _timer:NSTimer?

    // the start date of when the timer first started
    private var _startDate = NSDate()

    // the delegate used to implement the timerDidFire callback method
    weak var delegate:TimerDelegate?

    // start the timer with a given firing interval – which could be a property
    func startTimer(interval:NSTimeInterval) {

        // if timer already exists, make sure to stop it before starting another one
        if _timer != nil {
            stopTimer()
        }

        // reset start date and start new timer
        _startDate = NSDate()
        _timer = NSTimer.scheduledTimerWithTimeInterval(interval,
                                                        target: self,
                                                        selector: #selector(timerDidFire),
                                                        userInfo: nil, repeats: true)
    }

    // invalidate & deallocate the timer,
    // make sure to call this when you're done with the timer
    func stopTimer() {
        _timer?.invalidate()
        _timer = nil
    }

    // make sure to stop the timer when the wrapper gets deallocated
    deinit {
        stopTimer()
    }

    // called when the timer fires
    @objc func timerDidFire() {

        // get the change in time, from when the timer first fired to now
        let deltaTime = NSDate().timeIntervalSinceDate(_startDate)

        // do something with delta time

        // invoke the callback method
        delegate?.timerDidFire(deltaTime)
    }
}

然后您可以像这样使用它:

// your view controller class – make sure it conforms to the TimerDelegate
class ViewController: UIViewController, TimerDelegate {

    // an instance of the timer wrapper class
    let timer = TimerWrapper()

    override func viewDidLoad() {
        super.viewDidLoad()

        // set the timer delegate and start the timer – delegate should be set in viewDidLoad,
        // timer can be started whenever you need it to be started.
        timer.delegate = self
        timer.startTimer(1)
    }

    func timerDidFire(cumulativeTime: NSTimeInterval) {

        // do something with the total time

        let dateComponentsFormatter = NSDateComponentsFormatter()
        let text = dateComponentsFormatter.stringFromTimeInterval(cumulativeTime)
        label.text = text
    }
}

就这里使用NSTimer的适当性而言,因为你只使用1秒的时间间隔,NSTimer是合适的。通过将时间间隔超过总计时器持续时间,您可以平均任何小的射击不准确性。

答案 1 :(得分:0)

这是计时器的初始化方式

{{1}}