XCode - 动态创建的标签,当我更改文本时,它会更改为最后一个 [英] XCode - Dynamically created labels, when i change the text it changes it for the last one only

查看:95
本文介绍了XCode - 动态创建的标签,当我更改文本时,它会更改为最后一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一堆动态加载的标签..



每个都有相同的名称,因为没有告诉有多少会.. ..

我有另一个方法(不是创建标签的方法)改变其中一个标签的文本,但是当我运行它时,只有创建的最后一个标签会改变。



我需要它来更改具有某个标签或某事物的那个。



非常感谢,这个网站还没有让我失望。

解决方案

self.myLabel不能连接到多个标签,最后创建的标签的引用,你将不得不每次创建新的标签,并且你不能通过类属性跟踪它们,你必须通过他们的标签访问标签。



<您可以为每个标签设置标签,下面是示例代码,

  for(int i = 0; i< numberOfLabels; i ++)
{
UILabel * label = [[UILabel alloc] init];
label.tag = i; //不要使用标签0这里..可以使用i + 1或i + 100 ..这样的东西。
[self.view addSubview:label];
}

可存取标签,

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


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 the one that created the labels) changing the text for one of the labels, but when i run it only the last label that was created will change..

I need it to change the one that has a certain tag or something..

Help is much appreciated, this website is yet to let me down.

解决方案

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

这篇关于XCode - 动态创建的标签,当我更改文本时,它会更改为最后一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆