I want it so I can click a button and it changes the value in my array (and the background colour of the button) and then displays this value in a label.
I have no idea what’s going wrong. It seems I can’t access the array I set up in the viewDidLoad from the IBAction pushButtonOne because when i click it it just says “first 0” instead of “first 2” or “first 3”
How do I make it so I don’t have to declare the array in the pushButtonOne section??
In my .h
#import <UIKit/UIKit.h>
@interface ArrayPracticeViewController : UIViewController {
IBOutlet UIButton *buttonOne;
IBOutlet UIButton *buttonTwo;
NSMutableArray *array;
IBOutlet UILabel *arrayOne;
IBOutlet UILabel *arrayTwo;
}
-(IBAction)pushButtonOne;
-(IBAction)pushButtonTwo;
@property(nonatomic, retain)NSMutableArray *array;
@end
In my .m
#import "ArrayPracticeViewController.h"
@implementation ArrayPracticeViewController
@synthesize array;
-(void)viewDidLoad {
NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:[NSNumber numberWithInt:1],[NSNumber numberWithInt:2], nil];
arrayOne.text = [[NSString alloc] initWithFormat:@"first %d", [[array objectAtIndex:0] intValue]];
[super viewDidLoad];
}
-(IBAction)pushButtonOne{
if ([buttonOne.backgroundColor isEqual:[UIColor blackColor]]){
buttonOne.backgroundColor = [UIColor whiteColor];
[array replaceObjectAtIndex:0 withObject:[NSNumber numberWithInt:2]];
} else {
buttonOne.backgroundColor = [UIColor blackColor];
[array replaceObjectAtIndex:0 withObject:[NSNumber numberWithInt:3]];
}
arrayOne.text = [[NSString alloc] initWithFormat:@"first %d", [[array objectAtIndex:1] intValue]];
}
any help is much appreciated. Is theres a better way for me to store 0’s and 1’s in an array so that i can toggle their value from 0 to 1 or 1 to 0 by clicking a button??
Just to begin with and make sure you access the same array, change you viewDidLoad as below and see if that fixes