Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 将时间设置为uidatepickerview中的问题_Iphone_Objective C_Uilocalnotification_Uidatepicker_Alarm - Fatal编程技术网

Iphone 将时间设置为uidatepickerview中的问题

Iphone 将时间设置为uidatepickerview中的问题,iphone,objective-c,uilocalnotification,uidatepicker,alarm,Iphone,Objective C,Uilocalnotification,Uidatepicker,Alarm,我在应用程序中使用报警概念,我使用uilocalnotifications和datepickerview设置时间。我的日期选择器只显示时间 我用了密码 - (void)viewDidLoad { datePicker.date = [NSDate date]; } datepicker的时间设置为当前日期,但第二个值未设置为0; 它转到viewdidloaded的第二个值,即,对于ex,12:30:14 - (void)scheduleNotification { [[UIApplicatio

我在应用程序中使用报警概念,我使用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日期选择器

我尝试使用代码将第二个日期选择器设置为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;

}

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

您需要将
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];

这将很好地工作…

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

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