gpt4 book ai didi

ios - 如何获取 UIView 中的所有 UILabel

转载 作者:可可西里 更新时间:2023-11-01 03:28:11 24 4
gpt4 key购买 nike

我需要一种方法来获取所有 UILabelUIView 内无需遍历 UIView 中的所有 View .

我还有很多其他类型的观点,UIButton小号,UIImageView s,当我只需要 UILabel 时,遍历所有这些太长了

我正在努力避免这样的事情:

for (UIView *view in myView.subviews) 
{
if([view isKindOfClass:[UILabel class]])
{
UILabel *lbl = (UILabel*)view;
}
}

这是可能的还是我在做梦?

最佳答案

创建一个 NSArray ivar,您可以将所有 UILabel 添加到其中。

如果您在代码中执行此操作,那么在创建 UILabel 时就执行

NSMutableArray *labels = [[NSMutableArray alloc] init];

// Repeat this for al labels
UILabel *label = [[UILabel alloc] in...
[labels addObject:label];

// Then
self.labels = [labels copy];

如果您使用 IB,则像这样声明您的属性(property)

@property (nonatomic, strong) IBOutletCollection(UILabel) NSArray *labels;

并将所有标签连接到此。

关于ios - 如何获取 UIView 中的所有 UILabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10128124/

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