2016-01-04 23 views
-3

沒有顯示我有一些7 UILabels這將顯示日期象下面這樣:日期在正確的方式

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

另外我具有兩個在兩側箭頭UIButton時用戶可以移動到後日期或以前的日期。我有一個標籤,可以顯示當月基於我的日期顯示

這裏是我的全碼:

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) { 
      [email protected]"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個標籤我沒有得到正確的d吃了。而且在這段代碼中,當拉斯維加斯人擁有今天的日期時,我的左按鈕將被禁用。這樣

但我得到的結果:

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

預期的行爲是什麼?看到。 http://stackoverflow.com/help/mcve – user3743222

回答

0

試圖改變這樣的:

在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) { 
      [email protected]"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; 
      } 
     } 
    } 
} 

試試吧。並讓我知道它的作品

+0

是的,這工作......這應該是一個正確的解決方案。 – user5735383