在特定日期重复UILocalNotification [英] Repeat UILocalNotification on Specific day

查看:100
本文介绍了在特定日期重复UILocalNotification的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要设置UILocalNotification,我只需要从DatePicker花几分钟时间,并且需要设置特定日期(如:Monday),并在每个星期一重复。

I need to set UILocalNotification, I just need to take hour and minute from DatePicker and need to set specific date ( like : Monday ) and repeat it on every Monday.

我有两个问题:

第一个;是否可以在日期选择器的日期部分只显示星期日的日名称?

First one ; is it possible to show only "day names" like "Sunday" on the date section of date picker?

第二个问题是如果我想为本地通知设定具体日期,那么正确的代码是什么?

Second question is ; what is the correct codes if i want to set specific date for local notification ?

感谢您的答案,这里是我的代码下面;

Thank you for the answers and here is my code below ;

-(IBAction) alarmButton:(id)sender {

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
dateFormatter.timeZone = [NSTimeZone defaultTimeZone];
dateFormatter.timeStyle = NSDateFormatterShortStyle;
dateFormatter.dateStyle = NSDateFormatterShortStyle;

NSString *dateTimeString = [dateFormatter stringFromDate: dateTimePickerSet1.date];
NSLog ( @"Alarm Set :%@" , dateTimeString);

[self scheduleLocalNotificationWithDate1: dateTimePickerSet1.date];

}

-(void)scheduleLocalNotificationWithDate1:(NSDate *)fireDate {
UILocalNotification *notification = [[UILocalNotification alloc] init];

notification.fireDate = fireDate;
notification.alertBody = @"Alarm Set !";

[[UIApplication sharedApplication] scheduleLocalNotification:notification];


推荐答案

问题1 => 我不明白你想要什么(但是如果您只想显示星期日,星期一,....至星期六等天数的名称,请使用 UIPickerView

Question 1 => I can not understand what you want. (But if you want to display only name of days such like sunday, monday,....to saturday then use UIPickerView.)

问题2 => YES 您可以获得特定日期的正确日期,您可以通过设置 localNotification.repeatInterval = NSWeekCalendarUnit;

为了实现它,还需要使用下面的一个(方法) / em>代码。此方法将返回特定选定日期的正确日期,您只需要通过作为方法参数,例如

For achieve it, also need to use following piece of (method's) code. This method will return correct date of specific selected day, you just need to pass day as parameter of method such like,


如果选择的日期是星期天,然后通过 1

如果选择的日期是星期一,然后传递 2







如果选择的日期是星期六,然后传递 7

If selected day is sunday then pass 1
If selected day is monday then pass 2
.
.
.
If selected day is saturday then pass 7

这里是方法代码:

-(NSDate *) getDateOfSpecificDay:(NSInteger ) day /// here day will be 1 or 2.. or 7
{
  NSInteger desiredWeekday = day
  NSRange weekDateRange = [[NSCalendar currentCalendar] maximumRangeOfUnit:NSWeekdayCalendarUnit];
  NSInteger daysInWeek = weekDateRange.length - weekDateRange.location + 1;

  NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit fromDate:[NSDate date]];
  NSInteger currentWeekday = dateComponents.weekday;
  NSInteger differenceDays = (desiredWeekday - currentWeekday + daysInWeek) % daysInWeek;
  NSDateComponents *daysComponents = [[NSDateComponents alloc] init];
  daysComponents.day = differenceDays;
  NSDate *resultDate = [[NSCalendar currentCalendar] dateByAddingComponents:daysComponents toDate:[NSDate date] options:0];
  return resultDate;
}

不要忘了设置 localNotification.repeatInterval = NSWeekCalendarUnit;

这篇关于在特定日期重复UILocalNotification的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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