//取消
- (void)shutdownClock:(int)clockID
{NSArray *localNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
for(UILocalNotification *notification in localNotifications)
{
if ([[[notification userInfo] objectForKey:@"ActivityClock"] intValue] == clockID) {
NSLog(@"Shutdown localNotification:%@", [notification fireDate]);
[[UIApplication sharedApplication] cancelLocalNotification:notification];
}
}
}
//添加
{
UILocalNotification *notif1 = [[UILocalNotification alloc] init];
notif1.fireDate = [NSDate dateWithTimeIntervalSinceNow:clockID];
notif1.soundName = UILocalNotificationDefaultSoundName;
notif1.alertBody = [NSString stringWithFormat:name];
//显示在icon上的红色圈中的数子
notif1.applicationIconBadgeNumber = 1;
//设置userinfo 方便在之后需要撤销的时候使用
NSString *strnum=[NSString stringWithFormat:@"%i",clockID];
NSDictionary *info = [NSDictionary dictionaryWithObject:strnum forKey:@"ActivityClock"];
notif1.userInfo = info;
[[UIApplication sharedApplication] scheduleLocalNotification:notif1];
[notif1 release];
}
时间的计算方法
NSDate* now = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponents = [gregorian components:(NSHourCalendarUnit
| NSMinuteCalendarUnit
| NSSecondCalendarUnit
) fromDate:now];
[gregorian release];
//当前的"小时"
NSInteger currentHour = [dateComponents hour];
NSInteger currentMin = [dateComponents minute];
//当前“分钟”
//24小时和闹钟时间的计算
int currentTotal = currentHour*3600 + currentMin * 60;
int alarmTotal = hour * 3600 + min *60;
ontimer = alarmTotal - currentTotal;
ontimer就是得到的时间,传给方法就可以了