以编程方式创建的 UILabel - 再次找到它?

发布于 2024-10-17 15:34:36 字数 1138 浏览 6 评论 0原文

在 viewDidLoad 中,我以编程方式创建了 26 个 UILabel(字母表中的每个字母一个),并将字母表中的字母放入每个标签中(第一个为 A,第二个为 B...)。

后来我想找到一个特定的标签(比如包含字母“F”的标签)。我知道我想要查找的字母及其在字母表中的索引(因此对于 F,我知道它的索引是 5,因为 F 是字母表中的第 6 个字母)

我怎样才能找到这个标签?

当然,我可以制作 26 个 IBOutlet 并引用它们,但这看起来很麻烦。

这就是我创建标签的方式:

// set letters
letters = [[NSArray alloc] initWithObjects:@"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M", @"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"W", @"X", @"Y", @"Z", nil];

int positionX = 0;
int positionY = 0;
for(int i = 0; i < [letters count]; i++) {
    /* a lot of positioning stuff is here (positionX and positionY is set. I have removed this to make the code look cleaner */ 

    UILabel *letterLabel = [[UILabel alloc] initWithFrame:CGRectMake(positionX, positionY, 50, 50)];
    letterLabel.text = [letters objectAtIndex:i];
    letterLabel.backgroundColor = [UIColor clearColor];
    letterLabel.font = [UIFont fontWithName:@"Helvetica" size:45];
    letterLabel.textAlignment = UITextAlignmentCenter;
    [self.view addSubview:letterLabel];
    [letterLabel release];
}

In viewDidLoad I have created 26 UILabels programmatically (one for each letter in the alphabet) and I have put the letters from the alphabet into each label (A in the first, B in the second...).

Later I would like to find one specific label (say, the label containing the letter "F"). I know both the letter I want to find and its index in the alphabet (so for F, I know that its index is 5 because F is the 6th letter in the alphabet)

How can I find this label?

Of course, I can make 26 IBOutlets and refer to them but this seems such a hassle.

This is how I create the labels:

// set letters
letters = [[NSArray alloc] initWithObjects:@"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M", @"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"W", @"X", @"Y", @"Z", nil];

int positionX = 0;
int positionY = 0;
for(int i = 0; i < [letters count]; i++) {
    /* a lot of positioning stuff is here (positionX and positionY is set. I have removed this to make the code look cleaner */ 

    UILabel *letterLabel = [[UILabel alloc] initWithFrame:CGRectMake(positionX, positionY, 50, 50)];
    letterLabel.text = [letters objectAtIndex:i];
    letterLabel.backgroundColor = [UIColor clearColor];
    letterLabel.font = [UIFont fontWithName:@"Helvetica" size:45];
    letterLabel.textAlignment = UITextAlignmentCenter;
    [self.view addSubview:letterLabel];
    [letterLabel release];
}

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

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

发布评论

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

评论(3

罗罗贝儿 2024-10-24 15:34:36

有两种基本方法可以做到这一点:

1. 创建数组

只需在视图控制器中创建一个 NSMutableArray 作为 ivar,然后在创建标签时添加标签。要查找 F,请使用 [myArray objectAtIndex:5]

2. 使用标签

创建标签时,设置其标签 ([letterLabel setTag:i])。然后,当您想要检索它时,请使用 [[self view] viewWithTag:5]

There are two basic ways to do this:

1. Create An Array

Simply make an NSMutableArray as an ivar in your view controller, then add the labels as they’re created. To find F, use [myArray objectAtIndex:5].

2. Use Tags

When you create the label, set its tag ([letterLabel setTag:i]). Then, when you want to retrieve it, use [[self view] viewWithTag:5].

鸵鸟症 2024-10-24 15:34:36

我能想到的两种方法:

  1. 将所有标签添加到 NSArray;

  2. 为所有标签设置 tag 属性,并为每个标签设置特定的已知值,并使用 [self.view viewWithTag:TheTagForTheLabel];

    访问它们

Two approaches I can think of:

  1. Add all your labels to an NSArray;

  2. Set the tag property for all the labels with a specific know value for each label and access them with [self.view viewWithTag:TheTagForTheLabel];

尐偏执 2024-10-24 15:34:36

我可能会创建一个 NSMutableDictionary,其中包含一个 NSString,其中包含字母作为键,关联的 UILabel 作为对象。

I probably would've created an NSMutableDictionary with an NSString containing the letter as the key and the associated UILabel as the object.

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