iOS: 如何用一个方法为多个标签设置文本

iOS: how to setText for many labels with a method

各位。 我的故事板中有 5 个标签,我想将其每个文本设置为我想要的任何内容。让我们看看

[cloud0 setText:@"Test0"];
[cloud1 setText:@"Test1"];
[cloud2 setText:@"Test2"];
[cloud3 setText:@"Test3"];
[cloud4 setText:@"Test4"];

现在,我使用 "setText" 方法的次数与我的标签数量一样多。 是否有任何方法可以调用一次,并将其每个文本设置为与提到的完全相同?

非常感谢。

使用 1 到 numberOfLabel(例如 1 到 5)为每个标签设置标签

-(void)methodSetLabel{
  for(NSInteger i=1;i<=5;i++) {
      UILabel *label=(UILabel *)[self.view viewWithTag:i];
      label.text=[NSString stringWithFormate:@"Test %d",i];
  }
}

在您想以编程方式设置标签的地方调用此方法

您可以使用IBOutletCollection并通过枚举集合来设置文本。

[arrayLabelCollection enumerateObjectsUsingBlock:^(UILabel *label, NSUInteger idx, BOOL *stop) {
        label.text = arrayText[idx];
 }];
for (UIView *view1 in self.view.subviews) {
        if ([view1 isKindOfClass:[UILabel class]]) {
            UILabel *lable = (UILabel *) view1;
            lable.text = @"Your Text";
        }
    }

试试这个