0

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);
}
2
  • Explains, because dtDate seems to be a local variable, and dtDateArray seems to be the same? That's totally unclear. Commented Jan 19, 2018 at 9:19
  • First try a much simpler example yourself, before you bring the question here. Otherwise you are just lazily dumping your problem on somebody else. Narrow it down to the simplest possible problem first. Per previous comment, If you want variables that persist between different methods, you should be using ivars or global variables. Commented Jan 19, 2018 at 15:27

4 Answers 4

1

Your method tapSegmentButtonAction: accesses dtDate without any declaration, indicating that it is either an instance or global variable.

Your method setupSegmentButtons declares a variable dtDate creating a local variable which hides any instance or global with the same name.

Instead of:

NSMutableArray *dtDate = [@[] mutableCopy];

you should try:

dtDate = [NSMutableArray new]; 

HTH

Sign up to request clarification or add additional context in comments.

Comments

1

dtDate is a local variable in your method..

if your are having a property for storing dtDate variable

you should perform

self.yourProperty = dtDate

Comments

0

// assumig it is writtten as global variable

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);
}

Comments

0

After I put the NSMutable Array at viewDidLoad, then it become Global

- (void)viewDidLoad
{
[super viewDidLoad];

dtDate = [[NSMutableArray alloc] init];
}

Don't really understand why, if anyone can explain, that will be great.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.