开发者

NSTimer with a menu bar app

开发者 https://www.devze.com 2022-12-19 22:31 出处:网络
I\'m working on a simple timer app, and I\'ve created a NSStatusItem with a menu and I have some NSTextField labels that updates the timer labels (http://cld.ly/e81dqm) but when I click on the status

I'm working on a simple timer app, and I've created a NSStatusItem with a menu and I have some NSTextField labels that updates the timer labels (http://cld.ly/e81dqm) but when I click on the status item the NSTimer stops (and stops updating the labels)..... how can I get around this problem?

EDIT: here's the code that starts the timer:

开发者_运维技巧timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerDidUpdate:) userInfo:nil repeats:YES];


You should add timer into MainRunLoop as given below:

NSRunLoop * rl = [NSRunLoop mainRunLoop];
[rl addTimer:timer forMode:NSRunLoopCommonModes];


I'm guessing the timer resumes as soon as you stop interacting with the NSStatusItem? (After the menu's dismissed & mouse button released).

The user interaction puts the main run loop into a mode where it doesn't update timers, so if your label has to continually update, you'll probably need to move the NSTimer and the label drawing to a separate process or another thread.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号