代码之家  ›  专栏  ›  技术社区  ›  ios

如何通过编程生成UIN标签并添加到视图?

  •  0
  • ios  · 技术社区  · 8 年前

    我的目标是:

    1. 生成N个UILabels

      UILabel*label0=[[UILabel alloc]init]

      UILabel*label1=[[UILabel alloc]init]

      UILabel*标签n=[[UILabel alloc]init];

    2. 使用addSubView将每个UILabel添加到视图中

    为了生成UIN标签(如果我做得不对,请纠正我),我声明 NSMutableDictionay 在里面 viewcontroller.h

    @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>>";
    
    2 回复  |  直到 8 年前
        1
  •  1
  •   trapper    8 年前

    @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
    
        2
  •  1
  •   Krishna Datt Shukla    8 年前

    只需这样做--

    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