我需要从代码中制作一堆按钮并将它们添加到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 的无法识别的选择器
谁能指出我做错了什么?
因为数组是不可变的。您已在@property定义中指定了此内容,但这并不能使其成为真(如果情节提要取消存档将其设置为不可变数组)。
加载视图后,我想您可以说:
self.buttonsArray = [self.buttonsArray mutableCopy];
然后你的代码应该可以工作了。