我正在尝试实施日历。我有一个年份标签和两个按钮,用于递增和递减日历年份。我得到了这个例外
(-[__NSDate timeIntervalSinceReferenceDate]: message sent to deallocated instance 0x147165f0)
单击按钮(增量或减量)时,我的要求是,如果我单击右键,我的年份应该增加,如果我单击左键,我的年份应该减少。我的例外行是
date = [cal dateByAddingUnit:NSCalendarUnitYear value:1 toDate:date options:0];
你可以帮忙解决这个问题吗?这是我的代码
- (void)viewDidLoad
{
[super viewDidLoad];
date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"YYYY"];
NSString *stringFromDate = [formatter stringFromDate:date];
yar1.text=stringFromDate;
}
- (IBAction)right:(id)sender {
NSCalendar *cal = [NSCalendar currentCalendar];
date = [cal dateByAddingUnit:NSCalendarUnitYear value:1 toDate:date options:0];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"YYYY"];
NSString *stringFromDate = [formatter stringFromDate:date];
yar1.text=stringFromDate;
}
- (IBAction)left:(id)sender {
NSCalendar *cal = [NSCalendar currentCalendar];
date = [cal dateByAddingUnit:NSCalendarUnitYear value:1 toDate:date options:0];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"YYYY"];
NSString *stringFromDate = [formatter stringFromDate:date];
yar1.text=stringFromDate;
}
答案 0 :(得分:0)
上面的代码看起来很好,我尝试了示例应用程序,它的工作正常。你合成了你的日期属性吗?如果您的房产类型很强,请交叉检查。