Проблемы с установкой времени для uidatepickerview

Я работаю с концепцией будильника в своем приложении, я использовал uilocalnotifications и datepickerview для установки времени. Мой datepicker показывает только время.

Я использовал код,

- (void)viewDidLoad {
datePicker.date = [NSDate date];
}

Время для средства выбора даты установлено как текущая дата, но второе значение не установлено на 0; он переходит ко второму значению viewdidloaded, например, 12:30:14.

- (void)scheduleNotification {
[[UIApplication sharedApplication] cancelAllLocalNotifications];
Class cls = NSClassFromString(@"UILocalNotification");

if (cls != nil) {
    UILocalNotification *notif = [[cls alloc] init];
notif.fireDate = [datePicker date];
notif.timeZone = [NSTimeZone defaultTimeZone];
notif.alertBody = @"My Alarm";
notif.alertAction = @"Show";
notif.soundName = @"Show.mp3";
notif.applicationIconBadgeNumber = 1;

NSDictionary *userDict = [NSDictionary dictionaryWithObject:reminderText.text
forKey:kRemindMeNotificationDataKey];

notif.userInfo = userDict;

[[UIApplication sharedApplication] scheduleLocalNotification:notif];

[notif release];

}

будильник настроен на воспроизведение w.r.t datepicker

Я попытался установить вторую датупикер на 0, используя код

 - (void)viewDidLoad {

NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];

NSDate *reqdate = [self.datePicker date];

NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit |   NSMonthCalendarUnit |  NSDayCalendarUnit )

   fromDate:reqdate];

NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit )

   fromDate:reqdate];


   NSDateComponents *dateComps = [[NSDateComponents alloc] init];


[dateComps setHour:[timeComponents hour]];

[dateComps setMinute:[timeComponents minute]];

[dateComps setSecond:[timeComponents 0]];

NSDate *itemDate = [calendar dateFromComponents:dateComps];

datePicker.date = itemDate;

}

Но если я внезапно поставлю будильник, уведомитель сработает, я не знал, что здесь происходит,


person Nazik    schedule 06.08.2012    source источник


Ответы (2)


Вам нужно установить notif.fireDate в будущем. Попробуйте, например, это

    // Get the current date
    NSDate *now = [NSDate date];
    //make it later
    NSDate *pickerDate =   [now dateByAddingTimeInterval: (60.0 * 5)]; // 5 minutes in the future
    // 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 - or manipulate it here to whatever time/date in the future you may like!
    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:0];
    NSDate *itemDate = [calendar dateFromComponents:dateComps];


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

Это будет хорошо работать...

person user387184    schedule 06.08.2012

Вы не используете свою переменную dateComponents. Вы устанавливаете itemDate только с timeComponents, поэтому дата будет, возможно, 1 января 0001 года.

Возможно, уведомление должно быть выполнено немедленно (fireDate уже в прошлом).

person Mundi    schedule 06.08.2012