在特定日期重复UILocalNotification

编程入门 行业动态 更新时间:2024-10-10 06:22:13
本文介绍了在特定日期重复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

本文发布于:2023-11-24 12:08:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1625144.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:日期   在特定   UILocalNotification

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!