使用日期选择器时间和手动日期输入

时间:2014-02-02 19:30:45

标签: ios datepicker localnotification

我需要为星期日创建一个本地通知(然后每周重复一次),但用户可以从日期选择器设置时间。 有人可以检查出了什么问题,并说出最好的方法是什么?

提前致谢。

-(IBAction) setAlarm:(id)sender {

NSDate *pickerDate = [dateTimePickerSet1 date];

NSCalendar *gregCalendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponent = [gregCalendar components:NSYearCalendarUnit  | NSMonthCalendarUnit| NSWeekCalendarUnit | NSWeekdayCalendarUnit fromDate:[NSDate date]];
NSDateComponents *timeComponents = [gregCalendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:pickerDate];

NSDateComponents *newComponents = [[NSDateComponents alloc]init];


[newComponents setWeekday:1];// Sunday   <<< i need help for that part i think!
[newComponents setMonth:[dateComponent month]];
[newComponents setYear:[dateComponent year]];
[newComponents setHour:[timeComponents hour]];
[newComponents setMinute:[timeComponents minute]];

NSDate *fireDate = [gregCalendar dateFromComponents:newComponents];

1 个答案:

答案 0 :(得分:0)

我需要做一段时间才能找到这个答案。 iPhone SDK, how to get NSDate object of coming friday's 20:00?

注意他如何计算一周中的某一天。它比人们想象的要复杂一点。但它对我有用。