1

此应用程序适用于 OSX。我正在使用 NSTimer 在一天中的某个时间运行一个方法。

    NSCalendar* myCalendar = [NSCalendar currentCalendar];
NSDateComponents* components = [myCalendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit
                                             fromDate:[NSDate date]];
[components setHour:9];
[components setMinute:30];
[components setSecond:0];
NSDate *date = [myCalendar dateFromComponents:components];

NSTimer *t = [[NSTimer alloc] initWithFireDate:date
                                      interval:0.0
                                        target:self
                                      selector:@selector(fired:)
                                      userInfo:nil
                                       repeats:NO];
[[NSRunLoop mainRunLoop] addTimer:t forMode:NSDefaultRunLoopMode];

我怎样才能让它每天重复?

4

2 回答 2

1
interval:86400
repeats:YES

或者

使用警报将事件添加到用户日历

或者

山狮本地通知 http://www.renssies.nl/2012/02/mountain-lion-the-new-notifications-center/

于 2013-04-29T16:36:26.520 回答
-1

NSTimer不适合您想要实现的目标!

相反,请查看EKReminder该类,或NSUserNotification在 OS X 10.8 中引入的类。

如果您必须针对 OS X 的早期版本,请寻找涉及 EKCalendar 的其他选项,或考虑为您处理日期数学的其他服务(IIRC,launchd 有时间的概念,所以应该有一些其他的高最重要的级别 API)。

有几个 WWDC 会议视频处理这些事情——包括“Event Kit 中的事件和提醒”,以及去年的“国际化提示和技巧”的部分内容。

于 2013-04-30T12:46:04.390 回答