2012-11-29 27 views
1

我有一个标签和两个箭头按钮。当我按下左箭头时,我希望显示比这一天少一天的时间。当我按下右箭头时,应该添加一天。标签没有用正确的日期刷新

此刻这是我的代码。

- (IBAction)addDay:(id)sender { 
    NSDateComponents *dayComponent = [[NSDateComponents alloc] init]; 
    dayComponent.day = 1; 
    NSCalendar *theCalendar = [NSCalendar currentCalendar]; 
    NSString *dateNow = _lblDate.text; 

    // Convert string to date object 
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
    [dateFormat setDateFormat:@"EE. d MMMM YYYY"]; 
    NSDate *date = [dateFormat dateFromString:dateNow]; 

    date = [theCalendar dateByAddingComponents:dayComponent toDate:date options:0]; 
    NSString *dateString = [dateFormat stringFromDate:date]; 
    _lblDate.text = dateString; 

} 

- (IBAction)deleteDay:(id)sender { 

    NSDateComponents *dayComponent = [[NSDateComponents alloc] init]; 
    dayComponent.day = 1; 
    NSCalendar *theCalendar = [NSCalendar currentCalendar]; 
    NSString *dateNow = _lblDate.text; 

    // Convert string to date object 
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
    [dateFormat setDateFormat:@"EE. d MMMM YYYY"]; 
    NSDate *date = [dateFormat dateFromString:dateNow]; 

    date = [theCalendar dateByAddingComponents:dayComponent toDate:date options:0]; 
    NSString *dateString = [dateFormat stringFromDate:date]; 
    _lblDate.text = dateString; 

} 

但由于某些原因或其他的日期是错误的。添加和删​​除日期时没有任何模式。

任何人都可以帮助我吗?

亲切的问候

+0

你试过NSLogging检查日期形成的正确值? –

+1

确切地说,日期错误? – Hyperbole

+0

转到模块化方法使用这个码 - (的NSString *)shortHandDate { \t如果(shortHandDateFormatter ==无){ \t \t shortHandDateFormatter = [[NSDateFormatter的alloc] INIT]; \t \t [shortHandDateFormatter setDateFormat:@“EE d MMMM yyyy”]; \t} \t return [shortHandDateFormatter stringFromDate:self]; } –

回答

0

您的附加码是正确的。然而,依靠字符串到日期的转换会产生问题。我建议你采用一个NSDate对象作为实例变量,然后在下一个或前一天更新这个实例变量。更新值后,您可以使用dateformatter从该日期检索字符串。现在,当您使用标签的文本生成日期时,它会产生问题。 所以这就是你如何能做到这

// currentDate is my instance variable. 
currentDate = [[NSDate date] retain]; 
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease]; 
[dateFormat setDateFormat:@"EE. d MMMM YYYY"]; 
dateLabel.text = [dateFormat stringFromDate:currentDate]; 

这就是如何使你的nextdayfunctionwill样子

(IBAction)nextDay:(id)sender { 

     NSDateComponents *dayComponent = [[NSDateComponents alloc] init]; 
     dayComponent.day = 1; 
     NSCalendar *theCalendar = [NSCalendar currentCalendar]; 

     NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease]; 
     [dateFormat setDateFormat:@"EE. d MMMM YYYY"]; 

     currentDate = [[theCalendar dateByAddingComponents:dayComponent toDate:currentDate options:0] retain]; 
     NSString *dateString = [dateFormat stringFromDate:currentDate]; 
     dateLabel.text = dateString; 
    } 

这里是前一天

enter code here 

    (IBAction)previousDay:(id)sender { 

     NSDateComponents *dayComponent = [[NSDateComponents alloc] init]; 
     dayComponent.day = -1; 
     NSCalendar *theCalendar = [NSCalendar currentCalendar]; 

     NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease]; 
     [dateFormat setDateFormat:@"EE. d MMMM YYYY"]; 

     currentDate = [[theCalendar dateByAddingComponents:dayComponent toDate:currentDate options:0] retain]; 
     NSString *dateString = [dateFormat stringFromDate:currentDate]; 
     dateLabel.text = dateString; 
    } 
0

我不知道这是主要的问题,但在- (IBAction)deleteDay:(id)senderdayComponent.day = 1;应该dayComponent.day = -1;

0

我看到两个问题。

  1. 实际上没有递减由一个天(貌似除了?)
  2. 没有更新在主线程的UI。

尝试做了这些改变:

- (IBAction)deleteDay:(id)sender { 

    NSDateComponents *dayComponent = [[NSDateComponents alloc] init]; 
    dayComponent.day = -1; 
    /* snip */ 
} 

而且......

[_lblDate performSelectorOnMainThread:@selector(setText:) withObject:dateString waitUntilDone:NO];

+0

是什么让你发问#2?这些方法在点击按钮时被调用。这将在主线上。 – rmaddy

+0

CYA的移动,以防万一这些方法可能会从另一个线程中调用(我过去曾经这样做过)。 OP还没有指定日期是如何错误的,所以我列出了所有我能想到的帮助。 – Hyperbole

0

要增加一天,你需要的addTimeInterval一天如下

NSDate *newDate1 = [date addTimeInterval:60*60*24]; 

要减去一天做

NSDate *newDate2 = [date addTimeInterval:-60*60*24];