0

特定の日にアラームをセットしたい。の値を設定する方法がわかりませんkCFCalendarUnitWeekday。これが私のコードです:

NSDateComponents *components = [[NSDateComponents alloc] init];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
    return;
localNotif.fireDate = selected;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.repeatCalendar = [NSCalendar currentCalendar];

if (isSun) {
    [components setWeekday:1];

    [localNotif setRepeatInterval:(NSInteger)components];
}

if (isMon) {
    [components setWeekday:2];
    [localNotif  setRepeatInterval:(NSInteger)components];  
}

ありがとうございました。

4

1 に答える 1

1

わかりました、正しい方向に導くためのコードを次に示します。

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorian components:NSWeekdayCalendarUnit|NSYearCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit fromDate:[NSDate date]];

    // this will set the weekday to sunday
[components setWeekday:1];
    // this is a new date on sunday
NSDate * newDate = [gregorian dateFromComponents:components];

newDate が過去のものかどうかを調べる必要があるため、起動しません。


実際にコードの準備が整いました:

// ... init code ...

// *** this the important date ***
localNotif.fireDate = dateOnASunday;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.repeatCalendar = NSWeekCalendarUnit;

... // add body etc. ...

[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];

このスニペットの周りにメソッドを作成し、パラメーターを使用して日付を渡して、再利用できるようにします。最初の発火日となる日付を入力するだけで、毎週繰り返されます。日曜日または水曜日に日付を渡すだけです。

次のように繰り返し間隔を設定します。

// repeats notification on a weekly basis
localNotif.repeatCalendar = NSWeekCalendarUnit;

repeatCalender プロパティは、列挙型であるNSCalendarUnit型です。

于 2011-04-16T08:38:02.697 に答える