2

NSArrayにNSStringとして時間を格納するオブジェクトがあります。NSDateの代わりにNSStringをたくさん表示することになったので、NSStringを選択しました。UILocalNotificationを設定しようとしています。私がやったことは:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.timeZone = [NSTimeZone systemTimeZone];
dateFormatter.dateFormat = @"h:mm a";

for (NSString *s in _times) {
    NSDate *date = [dateFormatter dateFromString:s];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *dateComponents = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit)
                                                   fromDate:date];
    NSDateComponents *timeComponents = [calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:date];
    NSDateComponents *dateComps = [[NSDateComponents alloc] init];
    [dateComps setDay:[dateComponents day]];
    [dateComps setMonth:[dateComponents month]];
    [dateComps setYear:[dateComponents year]];
    [dateComps setHour:[timeComponents hour]];
    [dateComps setMinute:[timeComponents minute]];

    NSDate *itemDate = [calendar dateFromComponents:dateComps];

    NSLog(@"%@", [dateFormatter stringFromDate:itemDate]);
    UILocalNotification *note = [[UILocalNotification alloc] init];
    note.alertAction = thePill.name;
    note.alertBody = thePill.note;
    note.fireDate = itemDate;
    note.timeZone = [[NSCalendar currentCalendar] timeZone];

    if (note.fireDate == nil) {
        NSLog(@"nil firedate");
    }

//        note.repeatInterval = NSDayCalendarUnit;

    NSData *data = [self archivePill:thePill];
    NSDictionary *infoDict = [NSDictionary dictionaryWithObject:data forKey:PILL_NOTIFICATION];
    note.userInfo = infoDict;

    [[UIApplication sharedApplication] scheduleLocalNotification:note];
}

itemDate時刻をNSLogするとき、それは私が欲しいものです。メソッドの最後に、ローカル通知はその時点ではなくすぐに発生します。私の日付が適切な時刻であるため、理由がわかりません。fireDateをその日付に設定しただけだと思いましたか?ありがとう。

4

2 に答える 2

4

すぐに通知を起動するには、使用する必要があります

presentLocalNotificationNow

それ以外の

scheduleLocalNotification
于 2015-09-24T07:52:57.270 に答える
4
// Fire notification immediately after 1 sec 
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:1];
localNotification.alertBody = @"Your alert message";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
于 2013-08-07T07:03:45.113 に答える