且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在iOS中设置闹钟?

更新时间:2023-01-29 19:01:09


  1. 首先在你的 xib ,(或代码)设置日期选择器模式:时间(默认为日期和时间)

  1. First on your xib, (or code) set the date picker mode: Time (Default is date & time)

系统假定fireate是当前日期,时间是用户选择的时间。这不是问题,因为您设置了重复间隔,因此它将起作用。我测试了它。

The system assumes that the firedate is the current date, and the time is the time the user have chosen. This is not a problem because you set a repeat interval so it will work. I have tested it.

UILocalNotification *localNotif = [[UILocalNotification alloc] init];
[localNotif setFireDate:datePicker.date];
[localNotif setRepeatInterval:NSDayCalendarUnit];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];


PS:这是一个好主意使用 NSDateComponents 类将秒设置为 0 ,以便将闹钟设置为在您的第一分钟响铃想。您可以查看:

PS: It would be a good idea to set the seconds to 0 using NSDateComponents class so as to set the alarm to ring at the first second of the minute you want. You can check the:

iOS中的本地通知。

您发布的教程关于如何做到这一点。

tutorial you posted on how to do this.