2014-09-12 25 views
0

这是用于安排警报的以下代码,并且我收到以下错误。它已经完美的工作,直到我试图为当前日期比较捡拾工具:UIDatePicker“copyWithZone”错误

[self.eventText resignFirstResponder]; 

NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar]; 

//Get the current date 
NSDate *pickerDate = [self.datePicker date]; 

//Unable to set notification for same day 
[datePicker setMinimumDate:[NSDate date]]; 

//Break the date up into components 
NSDateComponents *dateComponents = [calendar components: (NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:pickerDate]; 
NSDateComponents *timeComponents = [calendar components: (NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:pickerDate]; 

// Schedule the notification 
UILocalNotification* localNotification = [[UILocalNotification alloc] init]; 
localNotification.fireDate = datePicker; 
localNotification.alertBody = self.eventText.text; 
localNotification.alertAction = @"Show me the item"; 
localNotification.timeZone = [NSTimeZone systemTimeZone]; 
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1; 

NSDictionary *infoDict = [NSDictionary dictionaryWithObjectsAndKeys:@"Object 1", @"Key 1", @"Object 2", @"Key 2", nil]; 
localNotification.userInfo = infoDict; 

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; 

// Request to reload table view data 
[[NSNotificationCenter defaultCenter] postNotificationName:@"reloadData" object:self]; 

eventText.text = nil; 

[self dismissViewControllerAnimated:YES completion:nil]; 

接收以下错误:终止应用程序由于未捕获的异常NSInvalidArgumentException,原因:-[UIDatePicker copyWithZone:]: unrecognized selector sent to instance 0x10ba853f0。我在调试中很糟糕,还没有找到解决方案。

回答

1

您有在这一行就是问题所在,我相信

localNotification.fireDate = datePicker; 

您没有提供上述代码中定义“日期选择器”,但我的猜测是,你想键入“pickerDate”和而不是“datePicker”,它不符合NSCopying协议(并且不是NSDate),这就是错误引发的原因。

+0

谢谢你这么快的回复,很好的回答。 – 2014-09-12 10:18:19

相关问题