0

UIDatePickerから日付と時刻を取得し、その日付と時刻が到着したときにローカル通知を発行する必要があります。どうすればこれを行うことができますか?ありがとう!

4

2 に答える 2

3

このコードを使用してください

NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];

    // Get the current date
    NSDate *pickerDate = your date from date picker;

    // Break the date up into components
    NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit )
                                                   fromDate:pickerDate];
    NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit )
                                                   fromDate:pickerDate];
    // Set up the fire time
    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]];
    [dateComps setSecond:[timeComponents second]];
    NSDate *itemDate = [calendar dateFromComponents:dateComps];
    [dateComps release];


    UILocalNotification *localNotif = [[UILocalNotification alloc] init];
    if (localNotif == nil)
        return;

    localNotif.fireDate = itemDate;
    localNotif.timeZone = [NSTimeZone defaultTimeZone];
        // Notification details
        localNotif.alertBody = @"Write your text here";
    // Set the action button
    localNotif.alertAction = @"View";

    localNotif.soundName = UILocalNotificationDefaultSoundName;


    // Specify custom data for the notification
    NSStream *str=[NSString stringWithFormat:@"%d",cId];
    NSDictionary *infoDict = [NSDictionary dictionaryWithObject:str forKey:@"NotificationDate"];
    localNotif.userInfo = infoDict;

    // Schedule the notification
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];


    NSLog(@"The notifications are:%@",[[UIApplication sharedApplication]scheduledLocalNotifications]);
    NSLog(@"The %@",localNotif.userInfo);

    [localNotif release];
于 2013-03-13T05:33:17.307 に答える
1

self.datePicker.date対象の日付と時刻を格納するために使用します。

次に、現在の日付が目標の日付と等しいかどうかを確認するためにミリ秒ごとにポーリングする NSTimer を作成します。両方が等しくなると、アラートビューが表示されます。

于 2013-03-12T17:12:39.153 に答える