2013-12-23 45 views
0

我創建了一個帶有每日提醒/通知的小型iOS應用程序。用戶能夠選擇他/她將被通知的小時(和分鐘)。到目前爲止,一切正常。如果我將間隔設置爲1分鐘進行測試,則會顯示通知。UIDatePicker結果至日期添加時間間隔

NSDate *scheduled = [[NSDate date] dateByAddingTimeInterval:60*1]; 
NSDictionary* dataDict = [NSDictionary dictionaryWithObjectsAndKeys:scheduled,@"remindMe",@"Background Notification received",@"notifyMe",nil]; 
[self scheduleNotificationWithItem:dataDict]; 

但我無法添加小時和分鐘了我的UIDatePicker的。如果我使用reminderTime.date,則通知將在給定時間內設置爲今天。但是我需要在給定的時間每隔一天通知,直到用戶停止。

有沒有人有一個想法,我可以做到這一點?

回答

1

您可以使用NSDateComponents完成此任務。添加一天(明天)並適當設置小時和分鐘。

例子:

unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit; 
NSCalendar *calendar = [NSCalendar currentCalendar]; 

NSDateComponents *components = [calendar components:unitFlags fromDate:[NSDate date]]; 
components.day += 1; 
components.hour = 14; // replace this with the value from your picker 
components.minute = 30; // replace this with the value from your picker 

NSDate *alertDate = [calendar dateFromComponents:components]; 
+0

謝謝!它似乎是做這項工作 –