我有一个标签和两个箭头按钮。当我按下左箭头时,我希望显示当前比该日期少一天。当我按下右箭头时,应该添加一天。
目前这是我的代码。
- (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 :(得分:0)
我不确定这是主要问题,但在- (IBAction)deleteDay:(id)sender
中,dayComponent.day = 1;
应为dayComponent.day = -1;
。
答案 1 :(得分:0)
我看到了两个问题。
尝试进行这些更改:
- (IBAction)deleteDay:(id)sender {
NSDateComponents *dayComponent = [[NSDateComponents alloc] init];
dayComponent.day = -1;
/* snip */
}
和...
[_lblDate performSelectorOnMainThread:@selector(setText:) withObject:dateString waitUntilDone:NO];
答案 2 :(得分: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];
这就是你的下一个功能会如何看起来像
(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;
}
答案 3 :(得分:0)
要添加一天,您需要添加一天的TimeInterval,如下所示
NSDate *newDate1 = [date addTimeInterval:60*60*24];
要减去一天,请执行以下操作
NSDate *newDate2 = [date addTimeInterval:-60*60*24];