I have created a date picker programatically in my application . when i select any time from my datepicker and click on the add button that date gets added to the cell.detailtext
label of my previous controller and as well as that date gets added to the notificationfire date to which the notification should get fired.But i am getting a problem in firing the notification.When i set the date it displays the notification as soon as i click the add button .This is my code to get the date from picker.
This is my controller class in which i created my picker.
- (void)viewDidLoad {
time = [[NSString alloc] init];
CGRect pickerFrame = CGRectMake(0,40,0,0);
datePicker = [[UIDatePicker alloc] initWithFrame:pickerFrame];
datePicker.datePickerMode = UIDatePickerModeTime;
datePicker.date = [NSDate date];
[datePicker addTarget:self action:@selector(convertDueDateFormat) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:datePicker];
[datePicker release];
}
-(NSString *开发者_开发技巧)convertDueDateFormat{
NSDate *pickerDate = self.datePicker.date;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"hh:mm"];
NSString *dateString = [dateFormatter stringFromDate:pickerDate];
NSLog(@"this is check date:%@",dateString);
//here dateString is a string variable which stores the value of the picker.
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
dateFromString = [dateFormatter dateFromString:dateString];
NSLog(@"Now complete date: %@",dateFromString);
[dateFormatter release];
//dateFromString is an nsdate variable where i storing the nsdate
//by converting the string dateString into date
//dateFromString variable time and dateString variable time does not matches.
return dateString;
}
Then i am calling my notification method in my other controller where i have my save button
-(IBAction)save{
[self setNotification:timepicker.dateFromString message:@"message"];
}
timepicker is object of the class where datepicker is created.
How to make my datepicker selected time match with the current time so that the notification can be set at proper time.
Please help me in solving this problem.Thanks
EDIT:
I have added the code for the notification to receive notification in the appdelegate
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Alarm" message:notification.alertBody delegate:self cancelButtonTitle:nil otherButtonTitles:@"SnooZe",@"STOP",nil];
[alert show];
[alert release];
[[UIApplication sharedApplication]cancelLocalNotification:notification];
}
to the set the notification this is the code
-(void)setNotification :(NSDate *)dat message:(NSString *)str{
UILocalNotification *local=[[UILocalNotification alloc]init];
[local setAlertBody:str];
[local setFireDate:dat];
[local setSoundName:UILocalNotificationDefaultSoundName];
[[UIApplication sharedApplication] scheduleLocalNotification:local];
[local release];
}
this method is called in the save button action click
If you want to change when the selector gets fired, then change the event for the target on the picker.
If the date is not matching with the current date, then change the formatting of the date to make it match.
Why are you converting the date to a string, then modifying your date formatter (setting time zone) then converting your string back to a date? Why are there no date format components in your date formatter's format? Why don't you pass the NSDate from your picker directly to your set notification message?
精彩评论