在特定日期重复UILocalNotification

Ric*_*ick 6 iphone objective-c datepicker ios localnotification

我需要设置UILocalNotification,我只需要从DatePicker获取小时和分钟,并需要设置特定日期 (如:星期一)并在每个星期一重复.

我有两个问题:

第一 ; 是否可以在日期选择器的日期部分仅显示"日期名称",如"星期日"?

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

谢谢你的答案,下面是我的代码;

-(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];
Run Code Online (Sandbox Code Playgroud)

iPa*_*tel 8

问题1 =>我无法理解你想要什么.(但是如果你想只显示周日的名字,比如周日,星期一......到星期六那么请使用UIPickerView.)

问题2 => 是,您可以获得特定日期的正确日期,您可以按设置获得每个特定日期的通知localNotification.repeatInterval = NSWeekCalendarUnit;

为了实现它,还需要使用下面的(方法)代码.此方法将返回特定选定日期的正确日期,您只需将日期作为方法的参数传递,如,

如果选择日是星期日,则通过1
如果选择日是星期一,则通过2
.
.
.
如果选定的一天是星期六,那么通过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;
}
Run Code Online (Sandbox Code Playgroud)

别忘了设置 localNotification.repeatInterval = NSWeekCalendarUnit;