正确代码:
- (void)awakeFromNib {
[super awakeFromNib];
//
[self setupCalendar];
//
self.chinaCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
// 上一月
- (void)previousClicked:(id)sender
{
NSDate *currentMonth = self.calendar.currentPage;
// NSCalendar *chineseCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierChinese];
NSDate *previousMonth = [self.chinaCalendar dateByAddingUnit:NSCalendarUnitMonth value:-1 toDate:currentMonth options:0];
[self.calendar setCurrentPage:previousMonth animated:NO];
// QYLog(@"===%@",self.calendar.currentPage);
NSDateComponents *comp = [NSDate dateComponetsWithDate:previousMonth];
NSString* yearStr = [NSString stringWithFormat:@"%zd",comp.year];
NSString* monthStr = [NSString stringWithFormat:@"%zd",comp.month];
if (self.calendarChangeBlock) {
self.calendarChangeBlock(yearStr, monthStr);
}
}
// 下一月
- (void)nextClicked:(id)sender
{
NSDate *currentMonthDate = self.calendar.currentPage;
//
// NSCalendar *chineseCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierChinese];
//
NSDate *nextMonthDate = [self.chinaCalendar dateByAddingUnit:NSCalendarUnitMonth value:1 toDate:currentMonthDate options:0];
//
[self.calendar setCurrentPage:nextMonthDate animated:NO];
NSDateComponents *comp = [NSDate dateComponetsWithDate:nextMonthDate];
// NSLog(@"-----%d",comp.month);
NSString* yearStr = [NSString stringWithFormat:@"%zd",comp.year];
NSString* monthStr = [NSString stringWithFormat:@"%zd",comp.month];
if (self.calendarChangeBlock) {
self.calendarChangeBlock(yearStr, monthStr);
}
}
}
下面是错误代码:
// 上一月
- (void)previousClicked:(id)sender
{
NSDate *currentMonth = self.calendar.currentPage;
NSCalendar *chineseCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierChinese];
NSDate *previousMonth = [chineseCalendar dateByAddingUnit:NSCalendarUnitMonth value:-1 toDate:currentMonth options:0];
[self.calendar setCurrentPage:previousMonth animated:NO];
// QYLog(@"===%@",self.calendar.currentPage);
NSDateComponents *comp = [NSDate dateComponetsWithDate:previousMonth];
// NSLog(@"-----%d",comp.month);
NSString* yearStr = [NSString stringWithFormat:@"%zd",comp.year];
NSString* monthStr = [NSString stringWithFormat:@"%zd",comp.month];
if (self.calendarChangeBlock) {
self.calendarChangeBlock(yearStr, monthStr);
}
//
}
// 下一月
- (void)nextClicked:(id)sender
{
NSDate *currentMonthDate = self.calendar.currentPage;
//
NSCalendar *chineseCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierChinese];
//
NSDate *nextMonthDate = [chineseCalendar dateByAddingUnit:NSCalendarUnitMonth value:2 toDate:currentMonthDate options:0];
//
[self.calendar setCurrentPage:nextMonthDate animated:NO];
NSDateComponents *comp = [NSDate dateComponetsWithDate:nextMonthDate];
// NSLog(@"-----%d",comp.month);
NSString* yearStr = [NSString stringWithFormat:@"%zd",comp.year];
NSString* monthStr = [NSString stringWithFormat:@"%zd",comp.month];
if (self.calendarChangeBlock) {
self.calendarChangeBlock(yearStr, monthStr);
}
网友评论