UILocalNotification应该在每个工作日重复,但周末也会发生火灾 [英] UILocalNotification is supposed to repeat every weekday, but fires on weekends as well

查看:163
本文介绍了UILocalNotification应该在每个工作日重复,但周末也会发生火灾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UILocalNotification应该每周一次,周一到周五开火,但不是周末。我认为将通知的repeatInterval属性设置为NSWeekdayCalendarUnit可以实现此目的。可悲的是,我的通知仍在周末开火。谁有人建议为什么?这是我的代码:

I have a UILocalNotification that is supposed to fire once a day, Monday through Friday, but not on the weekend. I thought that setting the repeatInterval property of the notification to NSWeekdayCalendarUnit would accomplish this. Sadly for me, my notifications are still firing on the weekend. Can anyone suggest why? Here is my code:

UILocalNotification *localNotification = [[UILocalNotification alloc] init];

localNotification.alertAction = @"View";
localNotification.alertBody = NSLocalizedString(@"ALERT_MESSAGE", nil);
localNotification.soundName = UILocalNotificationDefaultSoundName;

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MM-dd-yyyy HH:mm"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"America/Toronto"]];

// Notification fire times are set by creating a notification whose fire date 
// is an arbitrary weekday at the correct time, and having it repeat every weekday
NSDate *fireDate = [dateFormatter dateFromString:@"01-04-2012 11:00"]; 

localNotification.fireDate = fireDate;
localNotification.repeatInterval = NSWeekdayCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
            break;

[localNotification release];


推荐答案

iOS中的工作日仅表示一周内的一天。它不具有工作周的含义,而不是周末。

Weekday in iOS just means a day inside of a week. It doesn't have the "work week" connotation as opposed to a weekend.

文档说它更清楚一点,建议你使用1-7作为单位:

The documentation says it a little clearer, by suggesting that you use 1-7 as the units:


NSWeekdayCalendarUnit

NSWeekdayCalendarUnit

指定工作日单位。

对应的值是kCFCalendarUnitSecond。等于kCFCalendarUnitWeekday。工作日单位是数字1到N(公历中N = 7,1表示星期日)。

The corresponding value is an kCFCalendarUnitSecond. Equal to kCFCalendarUnitWeekday. The weekday units are the numbers 1 through N (where for the Gregorian calendar N=7 and 1 is Sunday).

来源:< a href =http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSCalendar_Class/Reference/NSCalendar.html =noreferrer> http://developer.apple .com / library / ios / #documentation / Cocoa / Reference / Foundation / Classes / NSCalendar_Class / Reference / NSCalendar.html

正确设置来自的通知星期一到星期五,这里有一些骷髅代码。你将不得不执行5次,所以将它封装在fireDate参数的方法中是件好事。我已经展示了你如何在星期一这样做。

To properly set your notifications from Monday through Friday, here's some skeleton code. Yo'll have to execute it 5 times, so it'd be good to encapsulate it inside of a method that parameters for the fireDate. I've shown how you could do it for Monday.

UILocalNotification *notification = [[[UILocalNotification alloc] init] autorelease];

// Set this to an NSDate that is for the time you want, on Monday
notification.fireDate = fireDate;            

// Repeat every week
notification.repeatInterval = NSWeekCalendarUnit;

这篇关于UILocalNotification应该在每个工作日重复,但周末也会发生火灾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆