上个头文件方便下面的理解;
下面是UIDatePicker的一些简单的用法
//autoupdatingCurrentCalendar 自动调取本电脑中的时间格式
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
// Get the current date
NSDate *pickerDate = [self.datePicker date];
// Break the date up into components在建NsDateComponent时要先实例话一个Calendar,()括号里面事放需要输出的那些时间段,格局calendar的格式输出pickdate中的内容,选择()中的内容。(年月日)
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)
{
//在itemdate这个时间基础上在延迟100秒 addTimeInterval的这个方法呗注视了
//localNotif.fireDate =[itemDate dateByAddingTimeInterval:100];
localNotif.fireDate=itemDate;//激发消息推送的时间
localNotif.timeZone = [NSTimeZone defaultTimeZone];//消息推送的区时,如果fireDate采用GTM标准时间,timeZone可以至nil
// Notification details
localNotif.alertBody = [eventText text];//消息提示的内容
// Set the action button
localNotif.alertAction = @"View";
//以上两条时必须的 要不就无法显示推送消息
//是否需要其他的butten
// localNotif.hasAction=YES;
//一周循环一次
localNotif.repeatInterval=kCFCalendarUnitWeekday;
//推送消息的提示声音,不过楼主我没有实现,没听到声音 在研究中
localNotif.soundName = UILocalNotificationDefaultSoundName;
//在推出程序时图表又上边会右一个消息未读数。
localNotif.applicationIconBadgeNumber = 1;
// Specify custom data for the notification
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"someValue" forKey:@"someKey"];
//在nsloacnotication中userInfo是自带的字典属性,用来传递数据和标记的作用
localNotif.userInfo = infoDict;
// Schedule the notification添加application中的推送列表,,没有这句化就无法调用tableview方法
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
//马上推送[[UIApplication sharedApplication] presentLocalNotificationNow:noti];
[localNotif release];
- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif {
//点击消息推送时候会调用这个方法。。
// Handle the notificaton when the app is running
NSLog(@"Recieved Notification %@",notif);
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:nil message:[notif alertBody] delegate:self cancelButtonTitle:nil otherButtonTitles:@"good", nil] autorelease];
[alert show];
if(notif.applicationIconBadgeNumber){
app.applicationIconBadgeNumber=notif.applicationIconBadgeNumber-1;
}
}