我需要设置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];
答案 0 :(得分: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;
}
不要忘记设置localNotification.repeatInterval = NSWeekCalendarUnit;
答案 1 :(得分:0)
这是@ iPatel的解决方案,它也会影响通过datepicker传入的时间组件。 (我还更新了他的解决方案,以使用未弃用的最新日历单元。)
- (NSDate *) getFireDateForDayOfWeek:(NSInteger)desiredWeekday withTime:(NSDate *)time // 1:Sunday - 7:Saturday
{
NSRange weekDateRange = [[NSCalendar currentCalendar] maximumRangeOfUnit:NSCalendarUnitWeekday];
NSInteger daysInWeek = weekDateRange.length - weekDateRange.location + 1;
NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:NSCalendarUnitWeekday fromDate:[NSDate date]];
NSInteger currentWeekday = dateComponents.weekday;
NSInteger differenceDays = (desiredWeekday - currentWeekday + daysInWeek) % daysInWeek;
NSDateComponents *daysComponents = [[NSDateComponents alloc] init];
daysComponents.day = differenceDays;
NSDate *fireDate = [[NSCalendar currentCalendar] dateByAddingComponents:daysComponents toDate:[NSDate date] options:0];
NSDateComponents *timeComponents = [[NSCalendar currentCalendar] components: NSCalendarUnitHour | NSCalendarUnitMinute fromDate:time];
NSDateComponents *fireDateComponents = [[NSCalendar currentCalendar]
components: NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond
fromDate:fireDate];
fireDateComponents.hour = timeComponents.hour;
fireDateComponents.minute = timeComponents.minute;
fireDateComponents.second = 0;
NSDate *resultDate = [[NSCalendar currentCalendar] dateFromComponents:fireDateComponents];
// The day could be today but time is in past. If so, move ahead to next week
if(resultDate.timeIntervalSinceNow < 0) {
resultDate = [resultDate dateByAddingTimeInterval:60 * 60 * 24 * 7];
}
return resultDate;
}
答案 2 :(得分:0)
此解决方案也需要时间考虑,但它比Oren的简化要简单
-(NSDate *) dateOfSpecificDay:(NSInteger ) day withFireTime:(NSDate *)timeDate/// here day will be 1 or 2.. or 7 and timeDate the current day with the desired time
{
NSInteger desiredWeekday = day;
NSRange weekDateRange = [[NSCalendar currentCalendar] maximumRangeOfUnit:NSCalendarUnitWeekday];
NSInteger daysInWeek = weekDateRange.length - weekDateRange.location + 1;
NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:NSCalendarUnitWeekday fromDate:[NSDate date]];
NSInteger currentWeekday = dateComponents.weekday;
NSInteger differenceDays = (desiredWeekday - currentWeekday + daysInWeek) % daysInWeek;
if (differenceDays == 0) {
//Check if should register for same day or for next week
if ([timeDate compare:[NSDate date]] == NSOrderedAscending) {
//Should schedule for next week
differenceDays = daysInWeek;
}
}
NSDateComponents *daysComponents = [[NSDateComponents alloc] init];
daysComponents.day = differenceDays;
NSDate *resultDate = [[NSCalendar currentCalendar] dateByAddingComponents:daysComponents toDate:timeDate options:0];
return resultDate;
}
答案 3 :(得分:0)
以下是 swift 版本:
func calculateTheNExtFriday(inputTime: NSDate) -> NSDate {
let desiredWeekday = 6 // Friday
let weekDateRange = NSCalendar.currentCalendar().maximumRangeOfUnit(NSCalendarUnit.Weekday)
let daysInWeek = weekDateRange.length - weekDateRange.location + 1;
let currentWeekday = NSCalendar.currentCalendar().components(NSCalendarUnit.Weekday, fromDate: inputTime).weekday
let differenceDays = (desiredWeekday - currentWeekday + daysInWeek) % daysInWeek
let dateComponents = NSDateComponents()
dateComponents.day = differenceDays
let calculatedFridayDate = NSCalendar.currentCalendar().dateByAddingComponents(dateComponents, toDate: inputTime, options: NSCalendarOptions.init(rawValue: 0))
return calculatedFridayDate!
}
在这个例子中,我在函数内部使用了desiredWeekday,但你也可以将它作为参数使用。
注意:请记住,第1天对应于星期日,2 - &gt;星期一和 等等
答案 4 :(得分:0)
斯威夫特3:
func calculateSpecificDay(_ desiredWeekday: NSInteger, fromDate: Date = Date()) -> Date {
let weekDateRange = NSCalendar.current.maximumRange(of: .weekday)!
let daysInWeek = weekDateRange.lowerBound.distance(to: weekDateRange.upperBound) - weekDateRange.lowerBound + 1
let currentWeekday = NSCalendar.current.dateComponents([.weekday], from: fromDate).weekday
let differenceDays = (desiredWeekday - currentWeekday! + daysInWeek) % daysInWeek
var dateComponents = DateComponents()
dateComponents.day = differenceDays
let calculatedFridayDate = NSCalendar.current.date(byAdding: dateComponents, to: fromDate)
return calculatedFridayDate!
}
If selected day is sunday then pass 1 If selected day is monday then pass 2 . . . If selected day is saturday then pass 7