我編寫了一個應用程序,允許用戶在日期選擇器中輸入日期,並且該應用程序會在日期到達前36小時提醒用戶。我的問題是在dateFromString:
。我不知道該放什麼。下面是代碼:iPhone:從日期選擇器獲取日期
- (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: ];
localNotif.fireDate = [eventDate dateByAddingTimeInterval:-13*60*60];
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = @"Event tommorow!";
localNotif.alertAction = nil;
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 0;
[[UIApplication sharedApplication]presentLocalNotificationNow:localNotif];
return YES;
}
任何幫助非常感謝,謝謝!
編輯:這是我使用,以節省他們的日期選擇器輸入的日期是否有幫助一些額外的代碼:
- (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"];}
您已將事件日期作爲字符串...它將轉換爲日期本身...以這種格式@「mm // dd/yyyy」... –
噢,好的,謝謝。用戶自己輸入日期,這對我來說很棘手。你知道我將如何去獲取他們輸入的日期,並將其設置在'dateFromString'區域? – John
對不起,我沒有得到它...我不知道這個確切.. –