Dynamically update NSMenuItem from NSTimer while menu is open in swift?

So here's my situation, I have a little app that will track how long you have been working and let you know when it's time to take a break. I have an NSTimer that fires every second and updates some numbers (time remaining). The app is in the status bar and I want the remaining time to be displayed in a menu item from the status menu dropdown. Right now I have this NSMenuItem update and it works, but when you click on the icon in the menu bar to get to the menu, the time will not update dynamically while you are looking at it. If you change the focus and then return to the menu, the time will be saved correctly, but again, when the menu is open, it does not update.

Is there anyway I can get the MenuItem to update even when the menu is open in swift? There are several other threads with objective C solutions, but I haven't been able to quickly adapt them.

+3


source to share


1 answer


You must add your timer to NSRunLoop.



NSRunLoop.currentRunLoop().addTimer(yourTimer, forMode: NSRunLoopCommonModes)

      

+2


source







All Articles