传递 kCFCalendarUnitWeekday 的值

发布于 2024-11-02 05:18:40 字数 638 浏览 3 评论 0原文

我想在特定的一天设置闹钟。我不明白如何设置 kCFCalendarUnitWeekday 的值。这是我的代码:

NSDateComponents *components = [[NSDateComponents alloc] init];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
    return;
localNotif.fireDate = selected;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.repeatCalendar = [NSCalendar currentCalendar];

if (isSun) {
    [components setWeekday:1];

    [localNotif setRepeatInterval:(NSInteger)components];
}

if (isMon) {
    [components setWeekday:2];
    [localNotif  setRepeatInterval:(NSInteger)components];  
}

谢谢。

I want to set an alarm on a particular day. I don't understand how to set the value of kCFCalendarUnitWeekday. Here is my code:

NSDateComponents *components = [[NSDateComponents alloc] init];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
    return;
localNotif.fireDate = selected;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.repeatCalendar = [NSCalendar currentCalendar];

if (isSun) {
    [components setWeekday:1];

    [localNotif setRepeatInterval:(NSInteger)components];
}

if (isMon) {
    [components setWeekday:2];
    [localNotif  setRepeatInterval:(NSInteger)components];  
}

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

满栀 2024-11-09 05:18:41

好的,这里有一些代码可以让您朝着正确的方向前进:

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorian components:NSWeekdayCalendarUnit|NSYearCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit fromDate:[NSDate date]];

    // this will set the weekday to sunday
[components setWeekday:1];
    // this is a new date on sunday
NSDate * newDate = [gregorian dateFromComponents:components];

您仍然必须找出 newDate 是否是过去的,因此它不会触发。


您实际上已准备好代码:

// ... init code ...

// *** this the important date ***
localNotif.fireDate = dateOnASunday;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.repeatCalendar = NSWeekCalendarUnit;

... // add body etc. ...

[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];

围绕此代码片段创建一个方法,并使用参数传递日期,以便您可以重用它。您只需输入一个日期,该日期应该是第一次火灾日期,并且每周都会重复。您只需传入周日或周三的日期即可。

您可以这样设置重复间隔:

// repeats notification on a weekly basis
localNotif.repeatCalendar = NSWeekCalendarUnit;

repeatCalender 属性的类型为 NSCalendarUnit 这是一个枚举。

Ok here is some code to get you in the right direction:

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorian components:NSWeekdayCalendarUnit|NSYearCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit fromDate:[NSDate date]];

    // this will set the weekday to sunday
[components setWeekday:1];
    // this is a new date on sunday
NSDate * newDate = [gregorian dateFromComponents:components];

You still have to find out whether the newDate is in the past, so it won't fire.


You actually have your code ready:

// ... init code ...

// *** this the important date ***
localNotif.fireDate = dateOnASunday;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.repeatCalendar = NSWeekCalendarUnit;

... // add body etc. ...

[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];

Create a method around this snippet and pass the date with a parameter so you can reuse it. You only have to put in a date which should be the first fire date and it'll repeat every week. You just have to pass in a date on a Sunday or Wednesday.

You set a repeat interval like this:

// repeats notification on a weekly basis
localNotif.repeatCalendar = NSWeekCalendarUnit;

The repeatCalender property is of type NSCalendarUnit which is an enum.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文