I know there are some questions on here that are very much like this but I am not finding the info I need. I am adding a grid of UIButtons to my view in a for loop. Each time a new button is created, I call addTarget:self action:@selector(doYourThing:) forControlEvent:UIControlEventTouchUpInside
. If I only make one button, it works. But when I create a whole grid of buttons using the for loop, none of the buttons want to call the selector and instead I get EXC_BAD_ACCESS. Any suggestions? Thanks everyone.
- (void)viewDidLoad
{
[super viewDidLoad];
[titleLabel setText:[site title]];
// Do any additional setup after loading the view from its nib.
buttonArray = [[NSMutableArray alloc] initWithCapacity:250];
int i = 0;
int c = 0;
int index = 0;
for (c=0; c < [site columnCount]; c++) {
for (i = 0; i < [site rowCount]; i++) {
plotButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[plotButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
CGPoint origin = CGPointMake(60.0, 40.0);
[plotButton setTitle:@"Plot" forState:UIControlStateNormal];
[plotButton setFrame:CGRectMake(origin.x + (90.0 * c), origin.y + (45.0 * i), 90.0, 45.0)];
[plotButton setTag:index];
[buttonArray insertObject:plotButton atIndex:index];
NSLog(@"%d", [plotButton tag]);
index ++;
[[self view] addSubview:plotButton];
}
}
}
Here's my for loop. idk if it's dumb to nest them like that but I'm making a grid of buttons and that seemed the sensible way to get rows and columns done. Anyways, thanks.
Added exception breakpoint. Got: Catchpoint 2 (throw)Pending breakpoint 1 - "objc_exception_throw" resolved.