NSMutable 数组未在另一种方法中返回值
NSMutable Array not returning value in another method
我已经用一种方法创建了值并将其添加到 NSMutable 数组中。下面是代码
-(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 returns (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);
}
//假设写成全局变量
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);
}
你的方法tapSegmentButtonAction:
访问dtDate
没有任何声明,表明它是一个实例或全局变量。
你的方法setupSegmentButtons
声明一个变量dtDate
创建一个隐藏任何同名实例或全局变量的局部变量。
而不是:
NSMutableArray *dtDate = [@[] mutableCopy];
你应该试试:
dtDate = [NSMutableArray new];
HTH
dtDate 是您方法中的局部变量..
如果您有 属性 用于存储 dtDate 变量
你应该执行
self.yourProperty = dtDate
我把NSMutable数组放到viewDidLoad后,就变成了Global
- (void)viewDidLoad
{
[super viewDidLoad];
dtDate = [[NSMutableArray alloc] init];
}
不太明白为什么,如果有人能解释一下,那就太好了。
我已经用一种方法创建了值并将其添加到 NSMutable 数组中。下面是代码
-(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 returns (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);
}
//假设写成全局变量
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);
}
你的方法tapSegmentButtonAction:
访问dtDate
没有任何声明,表明它是一个实例或全局变量。
你的方法setupSegmentButtons
声明一个变量dtDate
创建一个隐藏任何同名实例或全局变量的局部变量。
而不是:
NSMutableArray *dtDate = [@[] mutableCopy];
你应该试试:
dtDate = [NSMutableArray new];
HTH
dtDate 是您方法中的局部变量..
如果您有 属性 用于存储 dtDate 变量
你应该执行
self.yourProperty = dtDate
我把NSMutable数组放到viewDidLoad后,就变成了Global
- (void)viewDidLoad
{
[super viewDidLoad];
dtDate = [[NSMutableArray alloc] init];
}
不太明白为什么,如果有人能解释一下,那就太好了。