Question

I want to add items to an NSMutableArray based on user input. I've already taken care of the switch for handling different places for the user to enter information (I did it using a UIAlertView to capture the text input of the user). Reading through this I know that it is only resetting the array every time. I enter something new and is only saving the last one, but I've tried a few different formats and can't seem to get it to add to the array vs. overwriting what's already been stored in it. Here is the statement that handles adding the objects

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
_tankFilterArray = [[NSMutableArray alloc] init];
_tankMovementArray = [[NSMutableArray alloc] init];
_tankLightsArray = [[NSMutableArray alloc] init];

NSString *newLightsString = [[NSString alloc] init];
NSString *newFiltrationString = [[NSString alloc] init];
NSString *newMovementString = [[NSString alloc] init];

switch (alertView.tag)
{
    case 0:
            newLightsString = [alertView textFieldAtIndex:0].text;
            NSLog(@"New Lighting Added: %@", newLightsString);
            [_tankLightsArray addObject:newLightsString];
            NSLog(@"Number of Lights Added: %lu", _tankLightsArray.count);
        //            [_tankLights reloadData];
        break;
    case 1:
        newFiltrationString = [alertView textFieldAtIndex:0].text;
        [_tankFilterArray addObject:newFiltrationString];
        NSLog(@"Number of Filtration Parts Added: %lu", _tankFilterArray.count);
        NSLog(@"New Filtration Added: %@", newFiltrationString);
        //            [_tankFiltration reloadData];
        break;
    case 2:
        newMovementString = [alertView textFieldAtIndex:0].text;
        [_tankMovementArray addObject:newMovementString];
        NSLog(@"Number of Filtration Parts Added: %lu", _tankMovementArray.count);
        NSLog(@"New Movement Added: %@", newMovementString);
        //            [_tankMovement reloadData];
    default:
        break;
}
}

And where it's declared in the header file:

@property (strong, nonatomic) NSMutableArray *tankLightsArray;
@property (strong, nonatomic) NSMutableArray *tankFilterArray;
@property (strong, nonatomic) NSMutableArray *tankMovementArray;

EDITED for clarity

Was it helpful?

Solution

I think whenever you are clicking the button its reinitializing the Array. Try to initialize it somewhere else.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top