1

我是 iOS 开发的新手(来自 Android 开发),我需要一些帮助。我正在使用Vurig 的库来显示自定义日历控件并在日历中标记多个日期。

问题是我想标记回历日期而不是公历。我怎样才能做到这一点?

到目前为止,我已经将回历日期转换为公历以在日历上标记日期,但它只标记一个日期,而我想在日历月中标记多个日期。

到目前为止的代码:

-(void)calendarView:(VRGCalendarView *)calendarView switchedToMonth:(int)month targetHeight:(float)targetHeight animated:(BOOL)animated {
    NSArray *dates;


    NSDate *date = [self addDates:calendarView andDay:6 andMonth:10];

//    if (month==8){
//       dates = [NSArray arrayWithObjects:[NSNumber numberWithInt:5],[NSNumber numberWithInt:25], nil];
//    }
//    if (month==9) {
//       dates = [NSArray arrayWithObjects:[NSNumber numberWithInt:1],[NSNumber numberWithInt:5], nil];
//       
//    }

    //if (month==[[NSDate date] month]) {

    NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:( NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSWeekCalendarUnit) fromDate:date];

    int gmonth = [dateComponents month];
    if (month == gmonth) {
        dates = [NSArray arrayWithObjects:date, nil];
    }

    [calendarView markDates:dates];


}

-(void)calendarView:(VRGCalendarView *)calendarView dateSelected:(NSDate *)date {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]]; [dateFormatter setDateFormat:@"dd MMMM"];

    NSLog(@"Selected date = %@",[dateFormatter stringFromDate:date]);

    //NSLog(@"Selected date = %@",date);
}


-(NSDate *)addDates:(VRGCalendarView *)calendarView andDay:(int)day andMonth:(int)month{
    // Then create an Islamic calendar
    NSCalendar *hijriCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSIslamicCalendar];

    // And grab those date components for the same date
    NSDateComponents *hijriComponents = [[NSDateComponents alloc] init];

    hijriComponents.day = day;
    hijriComponents.month = month;
    hijriComponents.year = 1434;

    NSDate *hdate = [hijriCalendar dateFromComponents:hijriComponents];


    // Create a Gregorian Calendar
    NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

    // Set up components of a Gregorian date
    NSDateComponents *gregorianComponents = [gregorianCalendar components:(NSDayCalendarUnit |
                                                                           NSMonthCalendarUnit |
                                                                           NSYearCalendarUnit)
                                                                 fromDate:hdate];
    // Create the date
    NSDate *date = [gregorianCalendar dateFromComponents:gregorianComponents];

    NSLog(@"[In Hijri calendar ->] Date is %@", date);
    return date;

}
4

1 回答 1

1

您需要做的就是将一个NSDates 数组传递给该markDates:方法。您不需要先转换为公历,因为 anNSDate只是一个时间戳,独立于日历、时区等。

我下载了 VURIG 日历示例项目,并用以下方法VRGViewController替换了该calendarView:switchedToMonth:targetHeight:animated:方法,这应该准确说明您需要什么:

-(void)calendarView:(VRGCalendarView *)calendarView switchedToMonth:(int)month targetHeight:(float)targetHeight animated:(BOOL)animated {
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSIslamicCalendar];
    NSDateComponents *components = [[NSDateComponents alloc] init];
    [components setDay:6];
    [components setMonth:10];
    [components setYear:1434];
    NSDate *date1 = [calendar dateFromComponents:components];

    [components setDay:10];
    NSDate *date2 = [calendar dateFromComponents:components];

    [calendarView markDates:@[date1,date2]];
}

这标志着日历上的 8 月 12 日和 8 月 16 日,虽然我不熟悉回历日历,但据我所知这是准确的。

于 2013-08-14T20:26:06.920 回答