索引 UILabels Objective-C

发布于 2024-10-05 15:14:38 字数 207 浏览 0 评论 0原文

我正在编写 iPhone 应用程序,需要 UILabels 方面的帮助。 一个例子: 我有 10 个标签,名为 Label1、Label2、Label3 等。 和 Label1.text = @"1"、Label2.text=@"2" 等。 有没有办法在 for 循环中做到这一点。喜欢 for(int i = 1, i<11,i++){Labeli.text = ...} ? 谢谢你的帮助。

i am programming an iphone application and need help with the UILabels.
An example:
I have 10 Labels named Label1, Label2, Label3 etc.
and Label1.text = @"1", Label2.text=@"2" etc.
is there a way to do it in a for-loop. Like
for(int i = 1, i<11,i++){Labeli.text = ...} ?
thx for helping.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光是把杀猪刀 2024-10-12 15:14:38

为每个标签设置标签并按如下方式访问值,

for(int i=1; i<=10;i++)
{
    UILabel *lab=(UILabel *)[self.view viewWithTag:i];
    [lab setText:[NSString stringWithFormat:@"%d",i]];
}

否则执行以下操作,

NSArray *labels=[NSArray arrayWithObjects: label1, label2, nil];

int i=1;

for(UILabel *label in labels) {
  [label setText:[NSString stringWithFormat:@"%d",i]];
  i++;
}

Set tag for each label and access the value as follows,

for(int i=1; i<=10;i++)
{
    UILabel *lab=(UILabel *)[self.view viewWithTag:i];
    [lab setText:[NSString stringWithFormat:@"%d",i]];
}

or else do as follows,

NSArray *labels=[NSArray arrayWithObjects: label1, label2, nil];

int i=1;

for(UILabel *label in labels) {
  [label setText:[NSString stringWithFormat:@"%d",i]];
  i++;
}
花伊自在美 2024-10-12 15:14:38

这是正确的 for 循环:

for(int i = 0; i < numLabels; i++) {
    UILabel *label = [[UILabel alloc] init];
    label.text = [NSString stringWithFormat:@"%i", i];
    // other label customizations here
    [myMutableArray addObject:label]
    [label release];
}

然后,稍后使用 [myMutableArray objectAtIndex:index]; 访问每个标签

Here's the proper for loop:

for(int i = 0; i < numLabels; i++) {
    UILabel *label = [[UILabel alloc] init];
    label.text = [NSString stringWithFormat:@"%i", i];
    // other label customizations here
    [myMutableArray addObject:label]
    [label release];
}

Then, later, access each label with [myMutableArray objectAtIndex:index];

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文