增加NSDate +1天方法Objective-C? [英] Increase NSDate +1 day method Objective-C?

查看:105
本文介绍了增加NSDate +1天方法Objective-C?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的方法,我使用在我的导航栏增加一天,并将日的名称设置为标题。我知道这是错误的,因为我设置今天变量每次它的调用。但是我不知道如何在每次调用此方法时增加+1天。

This is my method I use for increasing by one day in my navigationBar, and setting the name of the day as a title. I know it's wrong because I set "today" variable every time its called. But I can't figure out how to increase +1 day every time I call this method.

-(void)stepToNextDay:(id)sender
{
    today = [NSDate date];
    NSDate *datePlusOneDay = [today dateByAddibgTimeInterval:(60 * 60 * 24)];
    NSDateFormatter *dateformatterBehaviour = [[[NSDateFormatter alloc]init]autorelease];
    [dateFormatter setDateFormat:@"EEEE"];
    NSString *dateString = [dateFormatter stringFromDate:datePlusOneDay];
    self.navigationItem.title = datestring;
}


推荐答案

在您的视图控制器的属性(ivar,...)。

Store the date your are showing in a property (ivar, ...) of your view controller. That way you can retrieve the current setting when you go to the next day.

如果要可靠地添加日期,请使用NSCalendar和NSDateComponents获取1天

If you want to reliably add dates, use NSCalendar and NSDateComponents to get a "1 day" unit, and add that to the current date.

NSCalendar*       calendar = [[[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar] autorelease];
NSDateComponents* components = [[[NSDateComponents alloc] init] autorelease];
components.day = 1;
NSDate* newDate = [calendar dateByAddingComponents: components toDate: self.date options: 0];

这篇关于增加NSDate +1天方法Objective-C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆