线程 1:EXC_BAD_ACCESS(代码=1,地址=0x5146a345)
Thread1: EXC_BAD_ACCESS(code=1,address=0x5146a345)
我正在尝试实现日历。我有一个年份标签和两个用于从日历递增和递减年份的按钮。我在单击按钮(递增或递减)时遇到此异常(Thread1:EXC_BAD_ACCESS(代码=1,地址=0x5146a345))我的要求是,如果我单击右键,我的年份应该增加,如果我单击左键按钮我的年份应该减少。我的例外行是 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;
}
你需要一个日期组件来像这样更改日期,
-(NSDate *)offsetYear:(int)numOfYears date:(NSDate*)date
{
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
[offsetComponents setYear:numOfYears];
return [[NSCalendar currentCalendar] dateByAddingComponents:offsetComponents
toDate:date options:0]; }
将偏移量传递为右侧为 1,左侧为 -1
我正在尝试实现日历。我有一个年份标签和两个用于从日历递增和递减年份的按钮。我在单击按钮(递增或递减)时遇到此异常(Thread1:EXC_BAD_ACCESS(代码=1,地址=0x5146a345))我的要求是,如果我单击右键,我的年份应该增加,如果我单击左键按钮我的年份应该减少。我的例外行是 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;
}
你需要一个日期组件来像这样更改日期,
-(NSDate *)offsetYear:(int)numOfYears date:(NSDate*)date
{
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
[offsetComponents setYear:numOfYears];
return [[NSCalendar currentCalendar] dateByAddingComponents:offsetComponents
toDate:date options:0]; }
将偏移量传递为右侧为 1,左侧为 -1