我需要设置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];
5 回答
Question 1 => 我无法理解你想要什么 . (但是如果你只想显示像 sunday, monday,....to saturday 这样的天的名字,那就用UIPickerView.)
Question 2 => YES 您可以获得特定日期的正确日期,您可以通过设置
localNotification.repeatInterval = NSWeekCalendarUnit;
获取每个特定日期的通知为了实现它,还需要使用下面的(方法)代码 . 此方法将返回特定选定日期的正确日期,您只需要传递 day 作为方法的参数,如,
这是方法代码:
别忘了设置
localNotification.repeatInterval = NSWeekCalendarUnit;
这是@ iPatel的解决方案,它也会影响通过datepicker传入的时间组件 . (我还更新了他的解决方案,以使用未弃用的最新日历单元) .
这个解决方案也需要时间,但它比Oren的简化得多
这是 swift 版本:
在这个例子中,我在函数内部使用了desiredWeekday,但你也可以使用它作为参数 .
斯威夫特3: