-1

根据 Apple 规范,IB 对象将使用 Storyboard/Xib 定义。对于某些项目,这可能会导致定义一长串 UILabel、UIButtons 等。它们必须使用 IBOutlet 连接到代码。

例子:

@property (nonatomic, retain) IBOutlet UILabel *label1;
@property (nonatomic, retain) IBOutlet UILabel *label2;
...
@property (nonatomic, retain) IBOutlet UILabel *label998;
@property (nonatomic, retain) IBOutlet UILabel *label999;

有没有办法在源代码中识别 UILabel(或 UIBUtton 或 UIImage 等),并使用它来更改对象的内容/属性?

示例伪代码:

for (i=0;i<100;i++) {
        <label i>.text = @"...";
}

在上面的代码中,我想为所有标签分配一个字符串值。这怎么能做到,所以而不是一种识别标签的方法。

4

1 回答 1

2

You can set tag for UILabels in Interface Builder and later on identify them using tags. For example you can set tag from 1001 to 2000 for your 1000 labels and later on you can use this code to get the labels at runtime :-

for (NSInteger iTag= 1001; iTag<2001; iTag++) 
{
    UILabel *lblTest = (UILabel *)[self.view viewWithTag:iTag];
    lblTest.text = @"....";
}

In this way your first label will have a tag 1001 and last label will have a tag 2000 and you can use these labels in functions or in a loop.

于 2012-11-19T08:13:03.330 回答