2014-09-12 27 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

相關問題