我写了一个应用程序,会在日期即将到来时提醒用户。我有一个日期选择器,我使用此代码在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
。我不知道该放什么,我习惯用文字字段做这个,而不是采摘。非常感谢任何帮助,谢谢!
答案 0 :(得分:1)
如果您想从假设的UITextField
中获取文字,请将dateFromString:
参数设置为以下内容:
UITextField *textField;
[dateFormatter dateFromString:textField.text];
修改强>
如果您想在评论中描述UIDatePicker
的日期,可以执行以下操作:
UIDatePicker *picker;
[dateFormatter stringFromDate:picker.date];