2012-01-20 12 views
0

日付の到来をユーザに警告するアプリを書いた。私は私が私のviewDidLoad方法でこのコードを使用して入力された日付を実施し、保存した日付ピッカーがありますiOS:通知センターでアラートを発行する

- (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"];} 

私がしようとしています

- (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; 

}

:周りの36時間にこのコードを使用した日付より前に通知センターでアラートを持っています

私の問題はdateFromString:datePickerです。私は何をそこに置くべきかわからない、私はピッカーではなく、テキストフィールドでこれを行うのに慣れている。どんな助けでも大歓迎です、ありがとう!

+1

通知関連の質問を新しい質問に分割する必要があります。通常、このサイトの投稿ごとに1つの質問が必要です。 – esqew

+0

、ありがとう! – John

答えて

1

あなたは架空のUITextFieldからテキストを取りたい場合は、これらの線に沿って何かにdateFromString:パラメータを設定します。あなたはUIDatePickerから日付を取りたい場合は

UITextField *textField; 
[dateFormatter dateFromString:textField.text]; 

EDIT

あなたのコメントで説明しているように、次のようなことができます:

UIDatePicker *picker; 
[dateFormatter stringFromDate:picker.date]; 
+0

ペン先には実際にテキストフィールドがありません。データピッカーから直接データを取り込むことは可能ですか? – John

+0

@ Joey502私はあなたの新しい仕様に私の答えを更新しました。 – esqew

+0

大変ありがとうございます! – John

関連する問題