Question

I need to make a bunch of buttons from the code and add them in IBOutletConnection. I haven't been able to do that so far. When I do it in storyboard, it works just fine, but I cannot add the button to a collection programmatically. Here is my code:

.h

@property (nonatomic, retain) IBOutletCollection(UIButton)NSMutableArray *buttonsArray;

.m

-(void)createButton
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    [button addTarget:self
               action:nil
     forControlEvents:UIControlEventTouchDown];

    [self writeCloud:button];
    button.frame = CGRectMake(-50, 80, 90, 60);
    [self.view addSubview:button];
    [_buttonsArray addObject:button];
}

The error I get is at [_buttonsArray addObject:button]; saying:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI addObject:]: unrecognized selector sent to instance 0x14559050'

Can anyone point out what I am doing wrong?

Was it helpful?

Solution

Because the array is not mutable. You have specified this in the @property definition but that doesn't make it true (if the storyboard unarchive sets it to a non-mutable array).

After the view is loaded I guess you can say:

self.buttonsArray = [self.buttonsArray mutableCopy];

and then your code should work.

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