Hanz Cheah
Hanz Cheah

Reputation: 811

NSMutable Array not returning value in another method

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

Upvotes: 0

Views: 77

Answers (4)

Hanz Cheah
Hanz Cheah

Reputation: 811

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.

Upvotes: 0

Akhil
Akhil

Reputation: 170

dtDate is a local variable in your method..

if your are having a property for storing dtDate variable

you should perform

self.yourProperty = dtDate

Upvotes: 1

CRD
CRD

Reputation: 53000

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

Upvotes: 1

Nilesh Jha
Nilesh Jha

Reputation: 1636

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

Upvotes: 0

Related Questions