XCode - 动态创建的标签,当我更改文本时,它只会更改最后一个
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11578647/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
XCode - Dynamically created labels, when i change the text it changes it for the last one only
提问by Mattigins
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.
非常感谢帮助,这个网站还没有让我失望。
回答by waheeda
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.
self.myLabel 不能连接到多个标签,所以它会包含上次创建的标签的引用,你每次都必须创建新标签,并且你不能通过类属性跟踪它们,你必须通过它们的标签访问标签.
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];
回答by WaaleedKhan
Okay since you dont have any code to show i guess i have to speculate.
好的,因为你没有任何代码可以显示我想我必须推测。
What i understood is that you are creating Dynamic UILabels
in ur code and you want to access them. Since you have same name for all the UILabels
you might me loosing the previous UILabel
when every time you create a new UILabel
. So in order to keep track of how many UILabel
you created you must add them in an Array. Declare an NSMutableArray
in your viewController.h
file and make sure in the viewDidLoad
u allocate it like
我的理解是您正在UILabels
您的代码中创建 Dynamic并且您想访问它们。既然你对所有具有相同的名称UILabels
你可能会失去我以前UILabel
当你每次创建一个新的UILabel
。因此,为了跟踪UILabel
您创建的数量,您必须将它们添加到一个数组中。NSMutableArray
在你的viewController.h
文件中声明一个并确保在viewDidLoad
你分配它像
arrForLabels = [[NSMutableArray alloc]init];
Since it is an NSMutableArray
you can add object to it.
因为它是一个NSMutableArray
你可以向它添加对象。
So when u create a UILabel
make sure you add the same UILabel
in the Arrayas well
for Instance
因此,当UILabel
您创建一个时,请确保UILabel
在Array中也为 Instance添加相同的内容
[arrForLabels addObject:yourLabel];
you can try to NSLog
your Arrayto see its content.
您可以尝试查看NSLog
您的Array以查看其内容。
Now all youu got to do is to Create a weak link like that
现在你要做的就是创建一个这样的薄弱环节
UILabel *tempLabel = [arrForLabels objectAtIndex:1];
now tempLabel
will be the UILabel
to change text
现在tempLabel
将是UILabel
更改文本
tempLabel.text = @"My New Text";
It will work fine. Feel free to ask for any issues in it.
它会正常工作。随意询问其中的任何问题。