用nsarray和nsmutablearray填充nsstackView



我有一个堆栈可以填充一系列视图。

_countViewArray = [[NSArray alloc] init];
_countViewArray = @[self.a.view,self.b.view,self.c.view];
_stackView = [NSStackView stackViewWithViews:_countViewArray];

这很好。如果我想用可变的数组替换此数组?

我尝试了此代码的"动态"堆栈视图,最后在简单的数组中转换了可变数组,但不起作用:

_mutableCountViewArray = [[NSMutableArray alloc] init];
[_mutableCountViewArray addObject:@[self.a.view]];
if (caseCondition){
   [_mutableCountViewArray addObject:@[self.b.view]];
}
[_mutableCountViewArray addObject:@[self.c.view]];
_countViewArray = [_mutableCountViewArray copy];
_stackView = [NSStackView stackViewWithViews:_countViewArray];

在consolle中,如果我打印了可变阵列,我有:

(
    (
    "<NSView: 0x600000121ea0>"
),
    (
    "<NSView: 0x600000120780>"
,
    (
    "<NSView: 0x6000001235a0>"
)
)

如何解决?

问题是您要添加数组(包含一个视图)而不是视图...

记住,@[x]是定义包含x

NSArray的文字表达式

这样的行:

[_mutableCountViewArray addObject:@[self.a.view]];

应该成为:

[_mutableCountViewArray addObject:self.a.view];

(当然,这适用于您在接下来的几行中添加的每个对象...)


另外,作为旁注:

_countViewArray = [[NSArray alloc] init];

在您的第一个片段中是多余的,因为您在下一行中重新分配一个值...

最新更新