2012-01-23 11 views
2

は、私はピッカーで設定した日の3日前をオフに行くためにdateComps setDay:を編集する方法を疑問に思いました。私は-3で打ち込んだが、それはそのトリックをしていないようだ。 ご迷惑をおかけして申し訳ありません。ここに私のコードは次のとおりです。のiOS:日付コンポーネント問い合わせ

- (IBAction)scheduleNotifButton:(id)sender { 
    NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar]; 

    NSDate *pickerDate = [self.datePicker date]; 

    NSDateComponents *dateComponents = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) 
                fromDate:pickerDate]; 
    NSDateComponents *timeComponents = [calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) 
                fromDate:pickerDate]; 

    NSDateComponents *dateComps = [[NSDateComponents alloc] init]; 
    [dateComps setDay:[dateComponents day]]; 
    [dateComps setMonth:[dateComponents month]]; 
    [dateComps setYear:[dateComponents year]]; 
    [dateComps setHour:13]; 
    [dateComps setMinute:30]; 
    [dateComps setSecond:[timeComponents second]]; 
    NSDate *itemDate = [calendar dateFromComponents:dateComps]; 

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

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

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

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; 

} 
+0

すべての偉大な答えをありがとうございました!それぞれが完璧なので正しいものを選ぶのは難しいでしょう!どちらにしても、みなさん、本当にありがとう、あなたは私を大いに助けてくれました! – John

答えて

2

あなたが-3を入力したと言うとき、あなたはこの行を使用し意味ですか?

[dateComps setDay:[dateComponents day] - 3]; 

これは実際には機能します。

また、あなただけの日付コンポーネントに現在の年の設定により任意の来年発射するシステムを伝えることができます。現在の年は、pickerDateの日付コンポーネントの検索方法と同じです。

その後、ローカル通知のrepeatIntervalNSYearCalendarUnitに設定すると、毎年発行されます。

+1

1日が2または1から始まる場合はどうなりますか?これによりsetDay:が負の数で呼び出されます。それは有効ですか? – nielsbot

+2

@nielsbot +1私は非常に疑問を除いてgcampと同じコード行を投稿するつもりでした。 @gcamp 1 – darvids0n

+0

は、私はそれがアップルのドキュメントで使われている例([ここ](http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DatesAndTimes/Articles/dtCalendricalCalculations.html#を見つけました// apple_ref/doc/uid/TP40007836-SW3)、リスト10を参照してください。 – darvids0n

2

私はすべてを行う必要がありません、日付ピッカーによって返された日から3日間引くだと思いますか?

NSDate *pickerDate = [self.datePicker date] 
pickerDate = [ pickerDate dateByAddingTimeInterval:-3 * 24 * 60 * 60 ] ; // add this line 

日付コンポーネントを使用したバージョン:

- (IBAction)scheduleNotifButton:(id)sender { 
    NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar]; 

    NSDate *pickerDate = [self.datePicker date]; 

    { 
     NSDateComponent * deltaComponents = [ [ NSDateComponents alloc ] init ]; 
     deltaComponents.days = -3 ; 

     pickerDate = [ pickerDate dateByAddingComponents:deltaComponents ] ; 
    } 

    NSDateComponents *dateComponents = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:pickerDate]; 

    [dateComps setHour:13]; 
    [dateComps setMinute:30]; 

    NSDate *itemDate = [calendar dateFromComponents:dateComps]; 

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

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

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

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; 

} 
+1

また、午後1時30分にイベントの3日前にアラームが鳴りたいですか?なぜ秒を抽出して 'dateComps'にセットしたのか分かりません...' timeComps'の作成をスキップし、秒を0に設定するだけです。 – nielsbot

+0

大変ありがとうございます!だから、私は時間compsのすべてを設定することをスキップし、ちょうどそれにあなたのコードを追加する必要がありますか?申し訳ありませんまだ私は初心者です。 – John

+0

'24 * 60 * 60'は必ずしも1日(閏年、昼間の時間節約)に等しいとは限らないため、これは有効ではない可能性があります。グレゴリオ暦ではないカレンダーの詳細もあります。 – gcamp

1

ここでは、今日(NOW)から3日間を減算するためのコードです。あなたのニーズに合わせて調整することができます:

NSDate *today = [NSDate date]; 
    NSLog(@"NOW:%@",today); 
    NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar]; 
    NSDateComponents *todayComps = [gregorian components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit |NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:today]; 
    todayComps.day -=3; 
    NSDate *dateMinus3Days = [gregorian dateFromComponents:todayComps]; 
    NSLog(@"3 days before NOW:%@",dateMinus3Days); 

あなたはそれが複雑すぎます。 DateComponentsを変更して新しい日付に設定することができます。

1

使用dateByAddingComponents:toDate:options:

// ... 
NSDateComponents *offset = [NSDateComponents new]; 
[offset setDay:(-3)]; 
NSDate *date3DaysBefore = [calendar dateByAddingComponents:offset toDate:pickerDate options:0]; 
// ... 
関連する問題