【发布时间】:2012-01-20 00:42:19
【问题描述】:
我编写了一个应用程序,它会在日期临近时提醒用户。我有一个日期选择器,我在viewDidLoad 方法中使用此代码实现并保存了输入的日期:
- (void)viewDidLoad {
NSDate *storedDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"DatePickerViewController.selectedDate"];
[self.datePicker setDate:storedDate animated:NO];
}
这是保存数据的方法:
- (IBAction)dateChanged:(id)sender {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDate *selectedDate = [self.datePicker date];
[defaults setObject:selectedDate forKey:@"DatePickerViewController.selectedDate"];}
我正在尝试使用此代码在日期前 36 小时左右在通知中心发出警报:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"mm'/'dd'/'yyyy"];
NSDate *eventDate=[dateFormatter dateFromString:datePicker];
localNotif.fireDate = [eventDate dateByAddingTimeInterval:-13*60*60];
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = @"Event tomorrow!";
localNotif.alertAction = nil;
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 0;
[[UIApplication sharedApplication]presentLocalNotificationNow:localNotif];
return YES;
}
我的问题在dateFromString:datePicker。我不知道在里面放什么,我习惯于用文本字段而不是选择器来做这个。非常感谢任何帮助,谢谢!
【问题讨论】:
-
您应该将与通知相关的问题拆分为一个新问题,通常本网站上的每个帖子都应该提出一个问题。
标签: ios xcode notifications datepicker