I have created and add values into an NSMutable Array in one method. Below is the code
-(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);
}
But when button is click and call another method as below, the NSMutable Array dtDate returns (null), WHY?
-(void)tapSegmentButtonAction:(UIButton *)button {
//Can't get the NSMutable Array here
NSLog(@" Returns Null : %@",dtDate);
NSString *txtDate = dtDate[0];
//Returns NULL
NSLog(@"txtDate=%@",txtDate);
}
dtDateseems to be a local variable, anddtDateArrayseems to be the same? That's totally unclear.