我正在使用动作表选择器视图。它工作正常。但我想在local notification
中设置特定时间时触发time picker
,而不会触发local notification
。我用了这段代码:
enter code here
- (void)actionPickerDone {
if (nil != self.data) {
//send data picker message[self.target performSelector:self.action withObject:[NSNumbernumberWithInt:self.selectedIndex]];
}
else {
//send date picker message
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
// Get the current date
NSDate *pickerDate = [self.datePicker date];
// Break the date up into components
NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit )
fromDate:pickerDate];
NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit )
fromDate:pickerDate];
// Set up the fire time
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:[dateComponents day]];
[dateComps setMonth:[dateComponents month]];
[dateComps setYear:[dateComponents year]];
[dateComps setHour:[timeComponents hour]];
// Notification will fire in one minute
[dateComps setMinute:[timeComponents minute]];
[dateComps setSecond:[timeComponents second]];
NSDate *itemDate = [calendar dateFromComponents:dateComps];
[dateComps release];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = itemDate;
NSLog(@"%@what is this",itemDate);
localNotif.timeZone = [NSTimeZone defaultTimeZone];
NSLog(@"i8i8i88i");
// Notification details
//localNotif.alertBody = [eventText text];
// Set the action button
localNotif.alertAction = @"View";
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 1;
// Specify custom data for the notification
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"someValue" forKey:@"someKey"];
localNotif.userInfo = infoDict;
// Schedule the notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];
}
这段代码有什么问题。请帮助我。
答案 0 :(得分:6)
我执行了你的代码并且通知没有触发。然后我取消注释了localNotif.alertBody赋值语句,它确实触发了。如果您未指定alertBody,则本地通知不会显示为警报。此外,Idan提出的观点也是有效的。如果您的应用程序位于前台,则不会显示通知,而是您将在应用程序委托中获得回调,您应在其中显示带有消息notification.alertBody的警报以确保一致性。
希望这会有所帮助:)
PS你不需要你正在做的所有日历组件。记录它们,你会发现pickerDate和itemDate都是一样的。