NSTimer not firing when NSMenu is open in Swift

前端 未结 1 1338
忘掉有多难
忘掉有多难 2020-12-01 19:21

I have a timer that runs to restart an alarm once it goes off.

alarm = NSTimer.scheduledTimerWithTimeInterval(
    60 * minutesConstant + secondsConstant,
          


        
相关标签:
1条回答
  • 2020-12-01 20:11

    You just have to add your timer to the main runloop as follow:

    Swift 4.2

    RunLoop.main.add(timer, forMode: .common)
    

    Swift 3

    RunLoop.main.add(alarm, forMode: .commonModes)
    

    Swift 2.x

    NSRunLoop.mainRunLoop().addTimer(alarm, forMode: NSRunLoopCommonModes)
    
    0 讨论(0)
提交回复
热议问题