我已经在一个方法中创建了NSMutable Array并将其添加到NSMutable Array中。以下是代码
-(void)setupSegmentButtons {
NSInteger numControllers = 7;
NSDate *now = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *beginningOfThisWeek;
NSTimeInterval durationOfWeek;
[calendar rangeOfUnit:NSWeekCalendarUnit
startDate:&beginningOfThisWeek
interval:&durationOfWeek
forDate:now];
NSMutableArray *dtDate = [@[] mutableCopy];
NSDateComponents *comps = [calendar components:NSUIntegerMax fromDate:now];
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd/MM/YYYY"];
for (int i = 0; i<numControllers; i++) {
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(X_BUFFER+i*(self.view.frame.size.width-2*X_BUFFER)/numControllers-X_OFFSET, Y_BUFFER, (self.view.frame.size.width-2*X_BUFFER)/numControllers, HEIGHT)];
[navigationView addSubview:button];
NSString *dateString = [dateFormatter stringFromDate:[calendar dateFromComponents:comps]];
[dtDate addObject:dateString];
[button addTarget:self action:@selector(tapSegmentButtonAction:) forControlEvents:UIControlEventTouchUpInside];
++comps.day;
}
//****** Manage to display my array with all the dates
NSLog(@" Can get my array : %@",dtDate);
}
但是当点击按钮并调用另一个方法时,NSMutable Array dtDate返回(null),为什么?
-(void)tapSegmentButtonAction:(UIButton *)button {
//Can't get the NSMutable Array here
NSLog(@" Returns Null : %@",dtDate);
NSString *txtDate = dtDate[0];
//Returns NULL
NSLog(@"txtDate=%@",txtDate);
}
答案 0 :(得分:1)
您的方法tapSegmentButtonAction:
在没有任何声明的情况下访问dtDate
,表明它是实例或全局变量。
您的方法setupSegmentButtons
声明变量dtDate
创建一个隐藏任何实例或全局同名的局部变量。
而不是:
NSMutableArray *dtDate = [@[] mutableCopy];
你应该尝试:
dtDate = [NSMutableArray new];
HTH
答案 1 :(得分:1)
dtDate是您方法中的局部变量..
如果你有一个存储dtDate变量的属性
你应该执行
self.yourProperty = dtDate
答案 2 :(得分:0)
//假设它被写为全局变量
NSMutableArray *dtDate;
-(void)setupSegmentButtons {
NSInteger numControllers = 7;
NSDate *now = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *beginningOfThisWeek;
NSTimeInterval durationOfWeek;
[calendar rangeOfUnit:NSWeekCalendarUnit
startDate:&beginningOfThisWeek
interval:&durationOfWeek
forDate:now];
dtDate = [NSMutableArray alloc] init];
NSDateComponents *comps = [calendar components:NSUIntegerMax fromDate:now];
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd/MM/YYYY"];
for (int i = 0; i<numControllers; i++) {
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(X_BUFFER+i*(self.view.frame.size.width-2*X_BUFFER)/numControllers-X_OFFSET, Y_BUFFER, (self.view.frame.size.width-2*X_BUFFER)/numControllers, HEIGHT)];
[navigationView addSubview:button];
NSString *dateString = [dateFormatter stringFromDate:[calendar dateFromComponents:comps]];
[dtDate addObject:dateString];
NSLog(@"dtDate inside loop =%@",dtDate);// for checking wheter at ths instance You have value or not
button.tag = i;
[button addTarget:self action:@selector(tapSegmentButtonAction:) forControlEvents:UIControlEventTouchUpInside];
++comps.day;
}
//****** Manage to display my array with all the dates
NSLog(@" Can get my array : %@",dtDate);
}
-(void)tapSegmentButtonAction:(UIButton *)button {
//Can't get the NSMutable Array here
NSLog(@" Returns Null : %@",dtDate);
NSString *txtDate = dtDateArray[0];
//Returns NULL
NSLog(@"txtDate=%@",txtDate);
}
答案 3 :(得分:0)
将NSMutable数组放在viewDidLoad后,它变为Global
- (void)viewDidLoad
{
[super viewDidLoad];
dtDate = [[NSMutableArray alloc] init];
}
不要理解为什么,如果有人能够解释,那将是伟大的。