1

我在我的应用程序中使用警报概念,我使用 uilocalnotifications 和 datepickerview 来设置时间。我的日期选择器只显示时间。

我用了代码,

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

datepicker 的时间设置为当前日期,但第二个值不设置为 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];

}

闹钟设置为播放 wrt datepicker

我尝试使用代码将 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;

}

但是如果我突然设置警报,通知器会触发警报,我不知道这里发生了什么,

4

2 回答 2

1

你需要设置你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];

这会很好地工作......

于 2012-08-06T07:23:55.940 回答
1

你没有使用你的变量dateComponents。您正在使用 设置itemDate公正 timeComponents,因此日期可能是 0001 年 1 月 1 日。

通知可能会立即到期(fireDate过去)。

于 2012-08-06T07:23:58.467 回答