XCode - Dynamically created labels, when i change the text it changes it for the last one only

前端 未结 2 473
自闭症患者
自闭症患者 2021-01-29 14:30

So i have a bunch of dynamically loaded labels..

Each of them has the same name because there is no telling how many there will be..

I have another method (not t

2条回答
  •  遥遥无期
    2021-01-29 14:58

    self.myLabel cannot be connected to multiple labels, so it will contain the reference of last created label, you will have to create new label every time, and you can't track them by class properties, you have to access label by their tag.

    you can set tag for each label, below is sample code,

     for(int i=0; i< numberOfLabels; i++)
    {
        UILabel *label = [[UILabel alloc] init];
        label.tag = i; // do not use tag 0 here.. u can use i+1, or i+100.. something like this.
        [self.view addSubview:label];
    }
    

    to access labels,

    UILabel *label = (UILabel*)[self.view viewWithTag: labelTag];
    

提交回复
热议问题