gpt4 book ai didi

ios - 如何以编程方式将 UIButton 添加到 IBOutletCollection

转载 作者:行者123 更新时间:2023-12-01 19:02:46 25 4
gpt4 key购买 nike

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

。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];说:

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



谁能指出我做错了什么?

最佳答案

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

加载 View 后,我想您可以说:

self.buttonsArray = [self.buttonsArray mutableCopy];

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

关于ios - 如何以编程方式将 UIButton 添加到 IBOutletCollection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21779532/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com