4

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

-(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];

}

请告诉我出了什么问题,或者我在这个问题上停留了很多天我在谷歌上搜索了很多次但找不到任何好的方法请为我做点什么请帮助我......谢谢

4

1 回答 1

1

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

localNofi.repeatInterval = NSWeekdayCalendarUnit;

我已经在 iOS 6.0 模拟器上对其进行了测试,得到了所需的结果,如果它不起作用,请告诉我。

于 2013-04-03T10:49:40.703 回答