swift invalidate timer不起作用 [英] swift invalidate timer doesn't work

查看:963
本文介绍了swift invalidate timer不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在有这个问题几天了,我不知道我做错了什么。

I have this problem for a few days now and I don't get what I am doing wrong.

我的应用程序基本上只是创建一些计时器。我需要阻止他们并创造新的。但目前停止它们不起作用。

My application is basically just creating some timers. I need to stop them and create new ones. But at the moment stopping them doesn't work.

self.timer = NSTimer.scheduledTimerWithTimeInterval(timeInterval, target:self, selector: "timerDidEnd:", userInfo: "Notification fired", repeats: false)

这是我的计时器

func timerDidEnd(timer:NSTimer){
    createUnrepeatedAlarmWithUpdateInterval()
}

因为我的计时器不想停止我正在使用未重复的计时器并在它停止后自己启动它。

Because my timer didn't want to stop I am currently using the unrepeated timer and start it myself after it stopped.

func stopAlarm() {

    if self.timer != nil {
        self.timer!.invalidate()
    }
    self.timer = nil
    self.timer = NSTimer()
}

这就是我停止计时器的方式。

And that's how I stop my timer.

alarmManager.stopAlarm()
alarmManager.createUnrepeatedAlarmWithUpdateInterval()

我调用 stopAlarm() f在创建一个新计时器之前启动。

I call the stopAlarm() function before creating a new timer.

我真的不知道我做错了什么,所以我感谢每一个答案:)

I really don't know what I am doing wrong so I appreciate every answer :)

class AlarmManager: ViewController{

private var timer : NSTimer?
private var unrepeatedTimer : NSTimer?
private let notificationManager = NotificationManager()
private var current = NSThread()
private let settingsViewController = SettingsViewController()

func createRepeatedAlarmWithUpdateInterval(){

    var timeInterval:NSTimeInterval = settingsViewController.getUpdateIntervalSettings()

    if timer == nil{
    timer = NSTimer.scheduledTimerWithTimeInterval(timeInterval,
        target: self,
        selector: "repeatedTimerDidEnd:",
        userInfo: "Notification fired",
        repeats: true)
    }
}
func repeatedTimerDidEnd(repeatedTimer:NSTimer){
    ConnectionManager.sharedInstance.loadTrainings(settingsViewController.getServerSettings())
    createUnrepeatedAlarm(10)
}

func createUnrepeatedAlarm(timeInterval:Double){

    unrepeatedTimer = NSTimer.scheduledTimerWithTimeInterval(timeInterval,
        target: self,
        selector: "unrepeatedTimerDidEnd:",
        userInfo: "Notification fired",
        repeats: false)
}
func unrepeatedTimerDidEnd(unrepeatedTimer:NSTimer){
    notificationManager.createNotification(self, reminderType: NotificationManager.ITEMRATINGREMINDER)
    notificationManager.createNotification(self, reminderType: NotificationManager.ITEMREMINDER)
    print("UnrepeatedAlarm ended")
}

func stopAlarm(){
    print("StopAlarm triggered")
    if (timer != nil)
    {
        print("stoptimer executed")
        timer!.invalidate()
        timer = nil
    }

    if (unrepeatedTimer != nil)
    {
        unrepeatedTimer!.invalidate()
        unrepeatedTimer = nil
    }
}
}

这个类的整个代码。也许这有帮助:D

Thats the whole code of this class. Maybe that helps :D

推荐答案

安全启动和停止计时器的常用方法是

The usual way to start and stop a timer safely is

var timer : NSTimer?

func startTimer()
{
  if timer == nil {
    timer = NSTimer.scheduledTimerWithTimeInterval(timeInterval, target: self, selector: "timerFired", userInfo: nil, repeats: true)
  }
}

func stopTimer()
{
  if timer != nil {
    timer!.invalidate()
    timer = nil
  }
}

startTimer()只有在 nil stopTimer()只有当它不是 nil 时才会停止。

startTimer() starts the timer only if it's nil and stopTimer() stops it only if it's not nil.

你只需要在停止计时器之前创建/开始新的。

You have only to take care of stopping the timer before creating/starting a new one.

Swift 3 替换


  • NSTimer 计时器

  • NSTimer.scheduledTimerWithTimeInterval( with Timer.scheduledTimer(timeInterval:

  • selector:timerFired with selector:#selector(timerFired)

  • NSTimer with Timer,
  • NSTimer.scheduledTimerWithTimeInterval( with Timer.scheduledTimer(timeInterval:
  • selector: "timerFired" with selector: #selector(timerFired).

这篇关于swift invalidate timer不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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