gpt4 book ai didi

ios - 如何以编程方式生成 n UILabel 并添加到 View ?

转载 作者:行者123 更新时间:2023-11-28 21:05:04 25 4
gpt4 key购买 nike

我是 iOS 开发新手。

我的目标是:

  1. 生成 N 个 UILabel

    UILabel *label0 = [[UILabel alloc]init]

    UILabel *label1 = [[UILabel alloc]init]

    ...

    UILabel *label n = [[UILabel alloc]init];

  2. 使用 addSubView 将每个 UILabel 添加到 View

为了生成 N 个 UILabel(如果我做错了请纠正我),我在 viewcontroller.h

中声明了 NSMutableDictionay
@property (strong,nonatomic)NSMutableDictionary *uiLabelsDictionary;

在viewcontroller.m中

self.uiLabelsDictionary = [[NSMutableDictionary alloc]init];

int yOffset = 40;

for(int i=0;i<10;i++)
{
yOffset = yOffset+40;
[self.uiLabelsDictionary setObject:[[UILabel alloc]initWithFrame:CGRectMake(10, yOffset, self.view.frame.size.width, self.view.frame.size.width)] forKey:[NSString stringWithFormat:@"label%d",i]];
}

NSLog(@"%@",self.uiLabelsDictionary);

我卡在下一步了。如何将存储在字典中的每个 UILabel 读取为 UILabel 并添加到 View 中?

字典输出

label0 = "<UILabel: 0x7f83dbd08ba0; frame = (10 80; 320 320); userInteractionEnabled = NO; layer = <_UILabelLayer: 0x608000090680>>";
label1 = "<UILabel: 0x7f83dbc0f7b0; frame = (10 120; 320 320); userInteractionEnabled = NO; layer = <_UILabelLayer: 0x60800008e240>>";
label2 = "<UILabel: 0x7f83dbc15570; frame = (10 160; 320 320); userInteractionEnabled = NO; layer = <_UILabelLayer: 0x60800008e4c0>>";
label3 = "<UILabel: 0x7f83dbc159a0; frame = (10 200; 320 320); userInteractionEnabled = NO; layer = <_UILabelLayer: 0x60800008e5b0>>";
label4 = "<UILabel: 0x7f83dbc15c30; frame = (10 240; 320 320); userInteractionEnabled = NO; layer = <_UILabelLayer: 0x60800008e790>>";
label5 = "<UILabel: 0x7f83dbc16040; frame = (10 280; 320 320); userInteractionEnabled = NO; layer = <_UILabelLayer: 0x60800008ebf0>>";
label6 = "<UILabel: 0x7f83dbc162d0; frame = (10 320; 320 320); userInteractionEnabled = NO; layer = <_UILabelLayer: 0x60800008ef10>>";
label7 = "<UILabel: 0x7f83dbc16560; frame = (10 360; 320 320); userInteractionEnabled = NO; layer = <_UILabelLayer: 0x60800008e510>>";
label8 = "<UILabel: 0x7f83dbc167f0; frame = (10 400; 320 320); userInteractionEnabled = NO; layer = <_UILabelLayer: 0x60800008ec40>>";
label9 = "<UILabel: 0x7f83dbc16a80; frame = (10 440; 320 320); userInteractionEnabled = NO; layer = <_UILabelLayer: 0x60800008f460>>";

最佳答案

使用数组。

@property (nonatomic, strong) NSArray *labels;

NSMutableArray *labels = [NSMutableArray array];

for (int i=0; i<10; i++) {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 80+i*40, self.view.frame.size.width, self.view.frame.size.width)];
[self.view addSubview:label]; // Add the label to the view
[labels addObject:label]; // Keep track of the labels
}
self.labels = labels;

NSLog(@"%@", labels);

// Later on...
UILabel *label0 = self.labels[0]; // Get a reference to label 0

关于ios - 如何以编程方式生成 n UILabel 并添加到 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46210500/

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