设置uidatepicker视图的时间问题

时间:2012-08-06 06:50:53

标签: iphone objective-c uilocalnotification uidatepicker alarm

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

我使用了代码,

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

将datepicker的时间设置为当前日期,但第二个值未设置为0; 它转到viewdidloaded的第二个值,即ex,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

我尝试使用代码

将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;

}

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

2 个答案:

答案 0 :(得分: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];

那会很好用......

答案 1 :(得分:1)

您没有使用变量dateComponents。您只需使用itemDate设置timeComponents,因此日期可能是1月1日1月1日。

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