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;

}

但由于某种原因,日期是错误的。添加和删​​除日期时没有模式。

有谁能够帮助我 ?

亲切的问候

4

4 回答 4

0

我不确定这是主要问题,但在- (IBAction)deleteDay:(id)sender,dayComponent.day = 1;应该是dayComponent.day = -1;.

于 2012-11-29T16:42:07.627 回答
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];

于 2012-11-29T16:56:44.390 回答
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];

这就是你 nextday 函数的样子

   (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;
    }
于 2012-11-29T17:33:40.290 回答
0

要添加一天,您需要添加一天的TimeInterval,如下所示

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

要减去一天,请执行以下操作

NSDate *newDate2 = [date addTimeInterval:-60*60*24];
于 2012-11-29T17:47:21.223 回答