【问题标题】:Understanding NSCalendar Units (please explain me as if I were five years old)了解 NSCalendar 单位(请像我五岁时一样解释我)
【发布时间】:2011-09-16 07:14:19
【问题描述】:

我正在尝试弄清楚如何在特定日期触发本地通知。

我必须使用日历单位,但我找到的关于它们的解释很糟糕。各位能不能给我解释一下,好像我是五岁,它们代表什么区间?

我已经填写了我认为我知道的,如果我错了请纠正我并填写其他的。

NSEraCalendarUnit = ????? era? 
NSYearCalendarUnit = event will repeat once in a year at the same month, day and hour
NSMonthCalendarUnit = event will repeat once a month at the same day and hour
NSDayCalendarUnit = event will repeat every day at the same hour
NSHourCalendarUnit = event will repeat every hour at the same minute
NSMinuteCalendarUnit = event will repeat every minute at the same second
NSSecondCalendarUnit = event will repeat every second
NSWeekCalendarUnit = event will repeat the same day of week every week 
NSWeekdayCalendarUnit = ?????? can I make it repeat just specific days? I mean, monday to friday but not saturday and sunday?
NSWeekdayOrdinalCalendarUnit = ????? how do I use that?
NSQuarterCalendarUnit = when exactly will it repeat? every 3 months?
NSCalendarCalendarUnit = ?????????
NSTimeZoneCalendarUnit = ????????

这是我需要做的一个例子:

UILocalNotification* alarm = [[[UILocalNotification alloc] init] autorelease];
if (alarm)
{
    alarm.fireDate = aDate;
    alarm.timeZone = [NSTimeZone systemTimeZone];
    alarm.repeatInterval = WHAT DO I PUT HERE? TO MAKE IT REPEAT JUST SATURDAYS AND SUNDAYS?;
    alarm.soundName = @"alarm.caf";
    alarm.alertBody = @"hi there";
    [app scheduleLocalNotification:alarm];
}

我需要将本地通知编程为以下一种可能性:

  • 从周一到周五每天开火,周六和周日除外
  • 每周六和周日开火,没有其他日子
  • 每周在同一天重复

我应该为 repeatInterval 使用什么值来完成此操作?

谢谢

【问题讨论】:

    标签: iphone ios uilocalnotification


    【解决方案1】:

    我相信你想要的东西是做不到的。 This question 可能会提供更多详细信息。

    当然,您可以做两个单独的本地通知,都设置为每周重复一次,一个在周六触发,一个在周日触发。

    【讨论】:

    • 好主意,谢谢。你能解释一下我的问题上有问号的那些条目是什么吗? NSWeekdayCalendarUnit到底是什么? NSCalendarCalendarUnit 呢?苹果应该解雇这些写他们卑鄙文档的人。从质量上讲,他们的文档与他们的产品相反。
    【解决方案2】:

    问:从周一到周五每天开火,周六和周日除外

    A:只需获取五个 UILocalNotification 实例并设置从星期一到星期五的三个触发时间(触发日期的年、月、日是免费的,只要工作日是你想要的)。然后设置三个 repeatInterval NSWeekdayCalendarUnit.after 将它们安排到 UIApplication .

    (ps:你可以用同样的方法解决其他问题)希望它有效!

    【讨论】:

      【解决方案3】:

      我认为你的做法是错误的。

      这是我要对一个 5 岁的孩子说的话(如果他能在 5 岁时理解 obj/c,我会尽快雇用他)。

      你想完成什么?您想发出警报还是想在一周中的特定日子执行特定操作?

      他可能会说:“我不知道”,但在这里您实际上想在周六和周日运行特定操作。

      为此,您确实需要一个计时器,例如每天、每小时或每分钟都会运行,这并不重要,计时器只会触发检查动作是否会发生的动作.

      NSTimer* aTimer = [[NSTimer alloc] initWithFireDate:[NSDate date] 
                                                         interval:3600*24 // every 24 hours for example 
                                                           target:self 
                                                         selector:@selector(onTimerTriggered:) 
                                                         userInfo:nil 
                                                          repeats:YES];
      

      现在您需要实际检查一下我们是一周中的哪一天。最重要的是这是分开进行的。

      - (void)onTimerTriggered:(id)sender
      {
          // Create your calendar (gregorian is what you need)
          NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
      
          // Create your components specified to only keep track of the week day of today
          NSDateComponents *weekdayComponents = [gregorian components:NSWeekdayCalendarUnit fromDate:[NSDate date]];
      
          // now you know which day we are !! (careful in the US, sunday is first day of the week)
          int weekday = [weekdayComponents weekday];
      
          // trigger alarm if needed
          if (weekday == 3 || weekday == 5) // wednesday or saturday
          { ... } // trigger your alarm instantly or wait until a specific hour it's up to you
      }
      

      总而言之,将您的问题分解为更具体的行动。您想在不同的日子创建特定的操作吗?

      1. 查看星期几
      2. 行动!

      NS--CalendarUnit 仅用于将特定日期分解为 Apple 所称的 NSDateComponents,但这绝对是 Apple 处理日期分解的方式,并且不是为其他语言编写的.

      【讨论】:

      • 谢谢,但我的问题是不可能使用定时器大间隔发出警报。计时器仅在应用程序运行时工作。问题是这样的。有时应用程序会退出。然后,在应用程序退出之前,我需要使用 UILocalNotifications 对 iOS 进行编程,这是我知道将来可以在应用程序退出运行时触发事件的唯一方法。如果我不这样做,则不会触发事件。 UILocalNotifications 重复所需的周期是我在问题最后提到的那些。我没有看到的是如何使用您的答案来做到这一点。还是谢谢。
      • 确实很抱歉,我误解了您的问题。现在正确的解决方案超出了我的知识范围。
      猜你喜欢
      • 2018-03-27
      • 1970-01-01
      • 1970-01-01
      • 2015-04-14
      • 2019-05-26
      • 2014-04-10
      • 2014-03-16
      • 2018-12-03
      • 2014-02-16
      相关资源
      最近更新 更多