0

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.

geraldWilliam
  • 4,123
  • 1
  • 23
  • 35
  • 3
    You've likely got some error in your loop that constructs the buttons (since it's perfectly fine to call the same selector from several button). Please post the corresponding code. – DarkDust Feb 04 '12 at 20:19
  • 1
    You need to [add an exception breakpoint](http://stackoverflow.com/questions/4961770/run-stop-on-objective-c-exception-in-xcode-4). Then trigger the error, copy the stack trace of the exception, edit your question, and paste in the stack trace. – rob mayoff Feb 04 '12 at 20:29
  • do you release or nil-ot buttonArray somewhere else than dealloc? – vikingosegundo Feb 04 '12 at 20:42
  • it's an ARC project so I haven't manually retained or released anything – geraldWilliam Feb 04 '12 at 20:44

2 Answers2

2

Your code works fine when I replace [site columnCount] and [site rowCount] by an integer. My guess is that you have a problem in one of those two methods.

Jorge
  • 2,530
  • 1
  • 19
  • 28
0

I guess you called the [UIButon buttonWithType:...] constructor and then you did release the button... But I'm just guessing. You should write the code here in order to be sure.

See you!

Ricard Pérez del Campo
  • 2,387
  • 1
  • 19
  • 22