objective-c 中的组创建 UI 个对象

Group creation UI object in objective-c

我有这个代码。我想让它更紧凑。是否可以添加类似控件的组?

UILabel *controlFreeName = [[UILabel alloc] init];
controlFreeName.frame = CGRectMake(40.0f, 70.0f, 120.0f, 50.0f);
controlFreeName.text = @"Free";

UILabel *controlNoFreeName = [[UILabel alloc] init];
controlNoFreeName.frame = CGRectMake(40.0f, 110.0f, 120.0f, 50.0f);
controlNoFreeName.text = @"No free";

UILabel *controlEquipmentName = [[UILabel alloc] init];
controlEquipmentName.frame = CGRectMake(40.0f, 150.0f, 120.0f, 50.0f);
controlEquipmentName.text = @"Equipment";

UILabel *controlTypeName = [[UILabel alloc] init];
controlTypeName.frame = CGRectMake(40.0f, 190.0f, 180.0f, 50.0f);
controlTypeName.text = @"Type";

尝试这样的事情

NSArray * myArray = [[NSArray alloc]initWithObjects:@"Test1",@"Test2",@"Test2", nil];

for (int i = 0; i < 3; i++)
{
    UILabel * myLabel = [[UILabel alloc]initWithFrame:CGRectMake(10*5*i, 10*5*i, 100, 100)];
    myLabel.text = [myArray objectAtIndex:i];
    int r = arc4random() % 255;
    int g = arc4random() % 255;
    int b = arc4random() % 255;
    myLabel.backgroundColor = [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1];
    [self.view addSubview: myLabel];
}