我在我的应用程序中使用警报概念,我使用 uilocalnotifications 和 datepickerview 来设置时间。我的日期选择器只显示时间。
我用了代码,
- (void)viewDidLoad {
datePicker.date = [NSDate date];
}
datepicker 的时间设置为当前日期,但第二个值不设置为 0;它转到 viewdidloaded 的第二个值,例如,12:30:14
- (void)scheduleNotification {
[[UIApplication sharedApplication] cancelAllLocalNotifications];
Class cls = NSClassFromString(@"UILocalNotification");
if (cls != nil) {
UILocalNotification *notif = [[cls alloc] init];
notif.fireDate = [datePicker date];
notif.timeZone = [NSTimeZone defaultTimeZone];
notif.alertBody = @"My Alarm";
notif.alertAction = @"Show";
notif.soundName = @"Show.mp3";
notif.applicationIconBadgeNumber = 1;
NSDictionary *userDict = [NSDictionary dictionaryWithObject:reminderText.text
forKey:kRemindMeNotificationDataKey];
notif.userInfo = userDict;
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
[notif release];
}
闹钟设置为播放 wrt datepicker
我尝试使用代码将 datepicker 的第二个设置为 0
- (void)viewDidLoad {
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDate *reqdate = [self.datePicker date];
NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit )
fromDate:reqdate];
NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit )
fromDate:reqdate];
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setHour:[timeComponents hour]];
[dateComps setMinute:[timeComponents minute]];
[dateComps setSecond:[timeComponents 0]];
NSDate *itemDate = [calendar dateFromComponents:dateComps];
datePicker.date = itemDate;
}
但是如果我突然设置警报,通知器会触发警报,我不知道这里发生了什么,