我正在工作的loacl通知通知成功。但我不想设置从拾取器通知的firedate,我想设置用户想要的通知firetime。我已经给了文本字段,以输入时间,并希望设置为每个用户的时间着火日期。
假设用户进入下午1:00,那么通知将在今天下午1:00收到
下面是我的代码--
NSCalendar *calendar = [NSCalendar currentCalendar]; // gets default calendar
NSDateComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:[NSDate date]]; // gets the year, month, day,hour and minutesfor today's date
[components setHour:txtStartTime.text];
// [components setMinute:12];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [calendar dateFromComponents:components];
localNotification.timeZone = [NSTimeZone localTimeZone];
localNotification.alertBody = txtSubjectName.text;
localNotification.alertAction = @"Lecture Notification";
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
[self dismissViewControllerAnimated:YES completion:nil];
发布于 2013-11-15 14:58:53
您可以使用NSDateFormatter
将字符串转换为NSDate,然后将该NSDate用作fireDate。但是,只有当用户完全按照预期输入日期时,它才能正常工作。以下是它的工作原理的代码示例:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd hh:mm:ss a"];
NSDate *myDate = [df dateFromString: myDateAsAStringValue];
这是一个显示不同日期格式模式的网站,您可以创建自己的输入样式:http://waracle.net/iphone-nsdateformatter-date-formatting-table/
您应该使用UIDatePicker
让用户轻松地输入日期和时间。这里有一个教程解释了如何使用它:http://www.edumobile.org/iphone/iphone-programming-tutorials/add-datepicker-programmatically-and-display-date-in-iphone/
https://stackoverflow.com/questions/19995166
复制相似问题