2012-01-22 7 views
1

ユーザーは日付選択ツールで日付を入力できるアプリを作成し、ボタンをタップするとローカル通知をスケジュールします。唯一の問題は、ボタンをタップすると通知が発生することです。どんな助けでも大歓迎です!あなたは少し後で通知をスケジュールする方法の完全なコードを探しているならここだ(のような3秒を言う)iOS:簡単なローカル通知3日前

NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar]; 
NSDate *currentDate = [self.datePicker date]; 

NSDateComponents *dateComponents = [[NSDateComponents alloc] init]; 
[dateComponents setDay:-3]; 

NSDate *targetDate = [calendar dateByAddingComponents:dateComponents toDate:currentDate options:0]; 

[dateComponents release]; 

... 

答えて

4

:ここに私のコードです完全なコード:

注:あなたが画面の上部にUIApplicationデリゲートを通して処理する必要があるメッセージボックスは表示されません。

UILocalNotification *localNotification = [[UILocalNotification alloc] init]; 

NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar]; 
NSDate *currentDate = [[NSDate alloc] init]; 

NSDateComponents *dateComponents = [[NSDateComponents alloc] init]; 
[dateComponents setSecond: 3 ]; 

NSDate *targetDate = [calendar dateByAddingComponents:dateComponents toDate:currentDate options:0]; 

localNotification.fireDate = targetDate; 

localNotification.timeZone = [NSTimeZone defaultTimeZone]; 

localNotification.alertBody = @"Notified"; 
localNotification.alertAction = @"Show"; 
localNotification.soundName = UILocalNotificationDefaultSoundName; 
localNotification.applicationIconBadgeNumber = 1; 

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; 
+1

dateComponentsのすべてをそのコードに置き換えてから、targetDateを使用してローカル通知を設定してください。 –

+1

また、 'targetDate'が現在の日付よりも遅いことを確認する価値があるかもしれません。あなたが過去にローカル通知をしようとしたときの動作が何であるか分かりません。 –

+0

ありがとうございます!通知は発生しませんでしたが、現在のコードでOPを更新する必要がありますか? – John

1

:あなたが代わりに[NSCalendar dateByAddingComponents:toDate:options:]を使用する必要が

- (IBAction)scheduleNotifButton:(id)sender { 
     NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar]; 
NSDate *currentDate = [self.datePicker date]; 

NSDateComponents *dateComponents = [[NSDateComponents alloc] init]; 
[dateComponents setDay:-3]; 

NSDate *targetDate = [calendar dateByAddingComponents:dateComponents toDate:currentDate options:0]; 

     UILocalNotification *localNotif = [[UILocalNotification alloc] init]; 
     if (localNotif == nil) 
      return; 
     localNotif.fireDate = targetDate; 
     localNotif.timeZone = [NSTimeZone defaultTimeZone]; 

     localNotif.alertBody = @"Event is in 3 days!"; 
     localNotif.alertAction = nil; 

     localNotif.soundName = UILocalNotificationDefaultSoundName; 
     localNotif.applicationIconBadgeNumber = 0; 

     [[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; 

    } 
関連する問題