Search code examples
iosobjective-cparameter-passingibaction

Storing integers in an IBAction and passing them to another IBAction


I have a pause IBAction & a resume IBAction, both for buttons, but I have game points I collect over the duration of the game play. How can I store the different game points integer values in the pause IBAction & pass them to the resume IBAction method.

NOTE: The pause IBAction invalidates all the NSTIMERS, and the resume IBAction recreates them.


Solution

  • Just to assist, I found a quick solution....but I'm sure there's a cleaner method:

    In the vc.h:

    NSMutableArray storeVariables;
    

    In the vc.m

    int a, b, c, d;
    - (void)viewDidLoad
    {
     [super viewDidLoad];
     a=0;
     b=0;
     c=0;
     d=0;
     storeVariables = [[NSMutableArray alloc] init];
    }
    
    -(IBAction)pauseButton:(id)sender{
     a=3;
     b=4;
     c=5;
     d=6;
     [storeVariables addObject:[NSNumber numberWithInt:a]];
     [storeVariables addObject:[NSNumber numberWithInt:b]];
     [storeVariables addObject:[NSNumber numberWithInt:c]];
     [storeVariables addObject:[NSNumber numberWithInt:d]];
     NSLog(@"%d, %d, %d, %d", storeVariables[0], storeVariables[1], storeVariables[2], storeVariables[3]);
    }
    
    -(IBAction)resumeButton:(id)sender{
     a = [storeVariables[0]];
     b = [storeVariables[1]]
     c = [storeVariables[2]]
     d = [storeVariables[3]]
     NSLog(@"%d, %d, %d, %d", a, b, c, d);
     storeVariables = [[NSMutableArray alloc] init];
    }