1

我需要从代码中制作一堆按钮并将它们添加到 IBOutletConnection 中。到目前为止,我还无法做到这一点。当我在情节提要中执行此操作时,它工作得很好,但我无法以编程方式将按钮添加到集合中。这是我的代码:

。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];
}

我得到的错误是[_buttonsArray addObject:button];说:

*** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[__NSArrayI addObject:]:无法识别的选择器发送到实例 0x14559050”

谁能指出我做错了什么?

4

1 回答 1

3

因为数组是不可变的。您已经在 @property 定义中指定了这一点,但这并不能实现(如果情节提要取消归档将其设置为非可变数组)。

加载视图后,我想您可以说:

self.buttonsArray = [self.buttonsArray mutableCopy];

然后你的代码应该可以工作。

于 2014-02-14T12:35:49.973 回答