[英]Thread1: EXC_BAD_ACCESS(code=1,address=0x5146a345)
i am trying to implement calendar. 我正在尝试实施日历。 i have one label for year and two buttons for incrementing and decrementing year from calendar. 我有一个年份标签和两个按钮,用于递增和递减日历年份。 i am getting this exception (Thread1: EXC_BAD_ACCESS(code=1,address=0x5146a345)) while clicking button(increment or decrement)my requirement is if i click right button my year should increase as well as if i click left button my year should decrease. 我得到这个异常(Thread1:EXC_BAD_ACCESS(代码= 1,地址= 0x5146a345)),而点击按钮(增加或减少)我的要求是,如果我点击右键我的年份应该增加,如果我点击左按钮我的一年应该减少。 my exception line is date = [cal dateByAddingUnit:NSCalendarUnitYear value:1 toDate:date options:0];
我的异常行是date = [cal dateByAddingUnit:NSCalendarUnitYear value:1 toDate:date options:0];
could you please help to resolve this issue. 你能帮忙解决这个问题吗? this is my code 这是我的代码
- (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;
}
You need a date component to change the date like this, 你需要一个日期组件来改变这样的日期,
-(NSDate *)offsetYear:(int)numOfYears date:(NSDate*)date
{
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
[offsetComponents setYear:numOfYears];
return [[NSCalendar currentCalendar] dateByAddingComponents:offsetComponents
toDate:date options:0]; }
Pass the offset as 1 for right and -1 for left 将偏移量传递为1表示右侧,-1表示左侧
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.