如何在每周一,周二,周三设置闹钟

时间:2013-04-03 10:16:18

标签: iphone nsdate nscalendar

我希望每天都设置闹钟请帮助我 我很多天都很困惑。 我正在使用此代码

-(void)localNotificationWithData:(NSDate *)firDate timeinterval:(int)interval{

NSCalendar *calender = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDate *pickerDate = firDate;
NSLog(@"%@",pickerDate);

NSDateComponents *dateComponents = [calender components:( NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSWeekCalendarUnit)fromDate:pickerDate];
/*... here we get Weekdays throug loop....*/
int rand = arc4random()%100;
NSLog(@"%d",rand);
for (int i=0; i<[strMarkList length]; i++) {
    NSString *getselectedCell=[strMarkList substringWithRange:(NSRange){i,1}];
    NSLog(@"%i",[getselectedCell intValue]);
    if ([getselectedCell intValue]== 0) {
        [dateComponents setDay:1];
        [dateComponents setWeekday:1];
        NSDate *itemDate = [calender dateFromComponents:dateComponents];
        [self repeatWeekDayInterval:itemDate rand:rand];
    }else if ([getselectedCell intValue]== 1) {
        [dateComponents setDay:2];
        [dateComponents setWeekday:2];
        NSDate *itemDate = [calender dateFromComponents:dateComponents];
        [self repeatWeekDayInterval:itemDate rand:rand];
    }else if ([getselectedCell intValue]== 2) {
        [dateComponents setDay:3];
        [dateComponents setWeekday:3];
        localNofi.repeatInterval = kCFCalendarUnitWeekday+7;
        NSDate *itemDate = [calender dateFromComponents:dateComponents];
        [self repeatWeekDayInterval:itemDate rand:rand];
    }else if ([getselectedCell intValue]== 3) {
        [dateComponents setDay:4];
        [dateComponents setWeekday:4];
        NSDate *itemDate = [calender dateFromComponents:dateComponents];
        [self repeatWeekDayInterval:itemDate rand:rand];
    }else if ([getselectedCell intValue]== 4) {
        [dateComponents setDay:5];
        [dateComponents setWeekday:5];
        NSDate *itemDate = [calender dateFromComponents:dateComponents];
        [self repeatWeekDayInterval:itemDate rand:rand];
    }else if ([getselectedCell intValue]== 5) {
        [dateComponents setDay:6];
        [dateComponents setWeekday:6];
        NSDate *itemDate = [calender dateFromComponents:dateComponents];
        [self repeatWeekDayInterval:itemDate rand:rand];
    }else if ([getselectedCell intValue]== 6) {
        [dateComponents setDay:7];
        [dateComponents setWeekday:7];
        NSDate *itemDate = [calender dateFromComponents:dateComponents];
        [self repeatWeekDayInterval:itemDate rand:rand];
    }
}
/*.....end.....*/


// NOW LOCALNOTIFICATION FIRE

}

 -(void)repeatWeekDayInterval:(NSDate *)itemDate rand:(int)rand{
if (localNofi == nil)
    return;
localNofi.fireDate = itemDate;
NSLog(@"%@",itemDate);
localNofi.timeZone = [NSTimeZone defaultTimeZone];
localNofi.alertBody = @"Time To Weak Up";
localNofi.alertAction = @"View";
localNofi.soundName = @"alarm-clock-bell.caf";
localNofi.applicationIconBadgeNumber = 1;
localNofi.repeatInterval = NSWeekCalendarUnit;

identifiLclNoti = rand;
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"%d",identifiLclNoti] forKey:@"identifyKey"];
localNofi.userInfo = infoDict;
NSLog(@"%@",localNofi);
[[UIApplication sharedApplication] scheduleLocalNotification:localNofi];

}

请告诉我有什么问题或者我坚持这个问题很多天我在谷歌搜索很多时间但是找不到任何好办法请为我做点什么 请帮帮我......谢谢

1 个答案:

答案 0 :(得分:1)

我刚看到你的日程安排通知代码,它很好并且工作正常。只需要更改以下内容,每周弹出本地通知。

localNofi.repeatInterval = NSWeekdayCalendarUnit;

我已经在iOS 6.0模拟器上测试了它,得到了所需的结果,让我知道它是否对您不起作用。