【问题标题】:Group creation UI object in objective-c在objective-c中组创建UI对象
【发布时间】:2015-10-29 20:25:16
【问题描述】:

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

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";

【问题讨论】:

  • 你可以使用一个 for 循环和一个包含所有“文本”的 NSArray。

标签: ios objective-c ios7 ios8 controls


【解决方案1】:

试试这样的

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];
}

【讨论】:

    猜你喜欢
    • 2011-04-08
    • 1970-01-01
    • 2019-01-05
    • 1970-01-01
    • 1970-01-01
    • 2012-02-14
    • 1970-01-01
    • 2011-01-15
    • 2011-02-26
    相关资源
    最近更新 更多