gpt4 book ai didi

ios - 获取设置为 ON 的开关数量

转载 作者:行者123 更新时间:2023-11-28 19:04:00 24 4
gpt4 key购买 nike

我有下面这个方法。有什么方法可以计算打开的 UISwitch 数吗?谢谢!

while (i < numberOfAnswers) {
UISwitch *mySwitch = [[UISwitch alloc] initWithFrame:CGRectMake(10, y+spaceBetweenAnswers-5, 0, 30)];
mySwitch.tag = i;
[_answerView addSubview:mySwitch];
i++;
}

最佳答案

我认为保留对开关的引用会更好。

NSMutableArray *switches = [NSMutableArray array]; // You can do that as property

while (i < numberOfAnswers) {
UISwitch *mySwitch = [[UISwitch alloc] initWithFrame:CGRectMake(10, y+spaceBetweenAnswers-5, 0, 30)];
mySwitch.tag = i;
[_answerView addSubview:mySwitch];
i++;

[switches addObject:mySwitch];
}

然后您不必遍历 View 中的每个 subview ,但您可以只遍历开关数组。

int count = 0;

for (UISwitch *switch in switches)
{
if (switch.isOn)
{
count += 1;
}
}

关于ios - 获取设置为 ON 的开关数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22101648/

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