iOS:忽略年份的日期组件

发布于 2024-12-28 15:38:04 字数 1725 浏览 3 评论 0原文

我编写了一个应用程序,让用户在日期选择器上输入日期,然后他们将按下按钮在日期临近前 3 天安排本地通知。唯一的问题是,我如何编辑此代码以不考虑用户输入的年份?因为他们可能会输入类似 1980 的年份,而这显然已经过去了。

我尝试删除 [dateComps setYear:[dateComponentsyear]];,但这会导致通知立即触发。我还尝试从日期组件中删除该组件和 NSYearCalendarUnit,但发生了同样的事情。非常感谢任何帮助!这是我的代码:

- (IBAction)scheduleNotifButton:(id)sender {
    NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];

    NSDate *pickerDate = [self.datePicker date];

    NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit )
                                                   fromDate:pickerDate];
    NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit )
                                                   fromDate:pickerDate];

    NSDateComponents *dateComps = [[NSDateComponents alloc] init];
    [dateComps setDay:[dateComponents day]];
    [dateComps setMonth:[dateComponents month]];
    [dateComps setYear:[dateComponents year]];
    [dateComps setHour:13];
    [dateComps setMinute:30];
    [dateComps setSecond:[timeComponents second]];
    NSDate *itemDate = [calendar dateFromComponents:dateComps];

    UILocalNotification *localNotif = [[UILocalNotification alloc] init];
    if (localNotif == nil)
        return;
    localNotif.fireDate = itemDate;
    localNotif.timeZone = [NSTimeZone defaultTimeZone];

    localNotif.alertBody = @"Event is in 3 days!";
    localNotif.alertAction = nil;

    localNotif.soundName = UILocalNotificationDefaultSoundName;
    localNotif.applicationIconBadgeNumber = 0;

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];

}

I wrote an app lets the user enter in a date on a date picker, and they will press a button to schedule a local notification 3 days before the date is approaching. The only issue is, how would I edit this code to not factor in the year that the user types in? Because they may type in something like 1980 for the year, which obviously has already passed.

I tried deleting [dateComps setYear:[dateComponents year]];, but that causes the notification to fire immediately. I also tried deleting both that and NSYearCalendarUnit from the date components, but the same thing happens. Any help is much appreciated! Here is my code:

- (IBAction)scheduleNotifButton:(id)sender {
    NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];

    NSDate *pickerDate = [self.datePicker date];

    NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit )
                                                   fromDate:pickerDate];
    NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit )
                                                   fromDate:pickerDate];

    NSDateComponents *dateComps = [[NSDateComponents alloc] init];
    [dateComps setDay:[dateComponents day]];
    [dateComps setMonth:[dateComponents month]];
    [dateComps setYear:[dateComponents year]];
    [dateComps setHour:13];
    [dateComps setMinute:30];
    [dateComps setSecond:[timeComponents second]];
    NSDate *itemDate = [calendar dateFromComponents:dateComps];

    UILocalNotification *localNotif = [[UILocalNotification alloc] init];
    if (localNotif == nil)
        return;
    localNotif.fireDate = itemDate;
    localNotif.timeZone = [NSTimeZone defaultTimeZone];

    localNotif.alertBody = @"Event is in 3 days!";
    localNotif.alertAction = nil;

    localNotif.soundName = UILocalNotificationDefaultSoundName;
    localNotif.applicationIconBadgeNumber = 0;

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

人│生佛魔见 2025-01-04 15:38:04

根据要求:

如果您忽略输入的年份并将其替换为当前年份会怎样?

As requested:

What if you just ignore the year entered and substitute it with the current year?

彻夜缠绵 2025-01-04 15:38:04

您可以获取当前年份,并将其设置为您的 dateComps

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [calendar components:NSYearCalendarUnit fromDate:[NSDate date]];
[dateComps setYear:[components year]];

编辑:

NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:[dateComponents day]];
[dateComps setMonth:[dateComponents month]];

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [calendar components:NSYearCalendarUnit fromDate:[NSDate date]];
[dateComps setYear:[components year]];

[dateComps setHour:13];
[dateComps setMinute:30];
[dateComps setSecond:[timeComponents second]];
NSDate *itemDate = [calendar dateFromComponents:dateComps];

you can just get the current year, and set it to your dateComps

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [calendar components:NSYearCalendarUnit fromDate:[NSDate date]];
[dateComps setYear:[components year]];

EDIT:

NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:[dateComponents day]];
[dateComps setMonth:[dateComponents month]];

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [calendar components:NSYearCalendarUnit fromDate:[NSDate date]];
[dateComps setYear:[components year]];

[dateComps setHour:13];
[dateComps setMinute:30];
[dateComps setSecond:[timeComponents second]];
NSDate *itemDate = [calendar dateFromComponents:dateComps];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文