date没有以正确的方式显示

我有一些7 UILabels将显示如下所示的date:

  Jan,2016 (label) (button) < **29Dec 30Dec 31Dec 1Jan 2Jan 3Jan 4Jan** >(Button) 

另外我有两个箭头UIButton在两边,当用户可以移动到倒退date或以前的date。 我有一个标签显示当前的月份,根据我的date显示

这是我的完整代码:

Viewcontroller.h

 @property (weak, nonatomic) IBOutlet UILabel *flabel; @property (weak, nonatomic) IBOutlet UILabel *slabel; @property (weak, nonatomic) IBOutlet UILabel *tlabel; @property (weak, nonatomic) IBOutlet UILabel *folabel; @property (weak, nonatomic) IBOutlet UILabel *fivlabel; @property (weak, nonatomic) IBOutlet UILabel *sixlabel; @property (weak, nonatomic) IBOutlet UILabel *sevenlabel; 

Viewcontroller.m

 #import "ViewController.h" @interface ViewController (){ NSDate *firstdate; } @end @implementation ViewController @synthesize flabel; @synthesize slabel; @synthesize tlabel; @synthesize folabel; @synthesize fivlabel; @synthesize sixlabel; @synthesize sevenlabel; @synthesize dateLabel; @synthesize walletView; @synthesize leftBtn; @synthesize rightBtn; - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. // firstdate = [NSDate date]; // firstdate = [NSDate dateWithTimeInterval:-(5*86400) sinceDate:firstdate]; firstdate = [[NSCalendar currentCalendar] dateByAddingUnit:NSCalendarUnitDay value:-5 toDate:[NSDate date] options:nil]; // NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@"MMMM,yyyy"]; dateLabel.text = [dateFormat stringFromDate:[NSDate date]]; dateLabel.text = [dateFormat stringFromDate: firstdate]; [self dateChange]; } -(void)dateChange { NSArray *labelArray = @[flabel, slabel, tlabel, folabel, fivlabel,sixlabel,sevenlabel]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; NSCalendar *calendar = [NSCalendar currentCalendar]; dateFormatter.dateFormat = @"ddMMM"; for (NSInteger i = 0; i < 7; ++i) { NSDate *nextDate = [calendar dateByAddingUnit:NSCalendarUnitDay value:i toDate:firstdate options:nil]; UILabel *label = (UILabel *)labelArray[i]; label.text = [dateFormatter stringFromDate:nextDate]; if (i==6) { dateFormatter.dateFormat=@"MMM,yyyy"; dateLabel.text = [[dateFormatter stringFromDate:nextDate] capitalizedString]; NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@"yyyy-MM-dd"]; if ([[dateFormat stringFromDate:nextDate] isEqualToString:[dateFormat stringFromDate:[NSDate date]]]) { leftBtn.enabled = false; //It's the same day } else { leftBtn.enabled = true; } } } } - (IBAction)calRight:(id)sender { NSCalendar *calendar = [NSCalendar currentCalendar]; firstdate = [calendar dateByAddingUnit:NSCalendarUnitDay value:7 toDate:firstdate options:nil]; [self dateChange]; ////// } - (IBAction)calLeft:(id)sender { NSCalendar *calendar = [NSCalendar currentCalendar]; firstdate = [calendar dateByAddingUnit:NSCalendarUnitDay value:-7 toDate:firstdate options:nil]; [self dateChange]; } 

它已经工作了6个标签,但是当我添加7个标签我没有得到正确的date。 而且在这个代码中,当左边的button有今天的date时,我的左边button将被禁用。

但是我得到这样的结果:

  Jan,2016 (label) (button) < **30Dec 31Dec 1Jan 2Jan 3Jan 4Jan 17Dec** >(Button) 

尝试改变这样的:

在ViewDidload中:

 firstdate = [[NSCalendar currentCalendar] dateByAddingUnit:NSCalendarUnitDay value:-6 toDate:[NSDate date] options:nil]; 

//和下面的方法

 -(void)dateChange { NSArray *labelArray = @[flabel, slabel, tlabel, folabel, fivlabel,sixlabel,sevenlabel]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; NSCalendar *calendar = [NSCalendar currentCalendar]; dateFormatter.dateFormat = @"ddMMM"; for (NSInteger i = 0; i < 7; ++i) { NSDate *nextDate = [calendar dateByAddingUnit:NSCalendarUnitDay value:i toDate:firstdate options:nil]; UILabel *label = (UILabel *)labelArray[i]; label.text = [dateFormatter stringFromDate:nextDate]; if (i==6) { dateFormatter.dateFormat=@"MMM,yyyy"; dateLabel.text = [[dateFormatter stringFromDate:nextDate] capitalizedString]; NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@"yyyy-MM-dd"]; if ([[dateFormat stringFromDate:nextDate] isEqualToString:[dateFormat stringFromDate:[NSDate date]]]) { leftBtn.enabled = false; //It's the same day } else { leftBtn.enabled = true; } } } } 

尝试一下 。 让我知道它的作品