【问题标题】:How to programatically generate n UILabels and add to View?如何以编程方式生成 n 个 UILabel 并添加到 View?
【发布时间】:2017-09-14 04:23:35
【问题描述】:

我是 iOS 开发的新手。

我的目标是:

  1. 生成N个UILabels

    UILabel *label0 = [[UILabel alloc]init]

    UILabel *label1 = [[UILabel alloc]init]

    ...

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

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

为了生成N个UILabels(如果我做错了请纠正我),我在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 并添加到视图中?

字典输出

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

【问题讨论】:

  • 您可能想问自己的问题是“我做的是否正确”。如果您尝试生成这么多标签,您可能希望它们位于 UITableView 或 UICollectionView 中。
  • @LucasDerraugh 谢谢你的回复。您在 UITableVIew 或 Collection View 中使用是正确的。我的意思是结合 NSDictionary 生成 n UIViewobject 是正确的方法
  • 在这里使用字典是一个糟糕的选择。使用NSMutableArray 会简单得多。
  • @rmaddy 谢谢你的回复。是否可以像在 NSDictionary 输出 label0 = ">"; 中那样映射 NSMutableArrray 中的每个 uilabel

标签: ios objective-c nsdictionary


【解决方案1】:

使用数组。

@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

【讨论】:

  • 谢谢您的回复。是否可以动态生成 UILabel *label0,UILabel *label1,UILabel *label2..
  • 你在数组里面有那个;标签[0],标签[1],标签[2] ...
  • UILabel *label0 = self.label[i] ,右边ok,左边呢,能不能动态生成UILabel label1,UILabel label2,...
  • 我不明白你的意思,也许可以作为一个新问题提出这个问题。
  • 你不能在objective-c中动态创建变量名(至少不能不搞乱运行时——你不想这样做)但是为什么你仍然需要label0,你有label[0] 就用那个。
【解决方案2】:

只需这样做 --

int yOffset = 40;

for(int i=0;i<10;i++) {
    yOffset = yOffset+40;
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, yOffset, self.view.frame.size.width, 30)];
    label.tag = i; //You can access any label by using tag
    [self.view addSubView:label];
}

====================

如果你想滚动页面,那么只需使用 UITableView

【讨论】:

  • 谢谢您的回复。我是新手,是否可以通过编程方式使用 UILabel *label1,UILabel *label2 。难道这只能通过标签来实现吗
  • 我的第二个疑问是分配对 *label 变量的引用 10 次。它会导致 *label 只指向最后一个 UIlabel 吗?
  • 不,它会创建 10 个不同的 UIlabel 对象。您可以使用 " UILabel *label = (UILabel *) [self.view viewWithTag:1]; " 访问它们中的任何一个
  • ARC 会自动释放其他 9 个对象,因为它没有引用吗?
  • 不,它们不会被解除分配。它们将被 ViewController 销毁
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-25
相关资源
最近更新 更多