以编程方式创建的 UILabel - 再次找到它?
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有两种基本方法可以做到这一点:
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]
.我能想到的两种方法:
将所有标签添加到
NSArray
;为所有标签设置
tag
属性,并为每个标签设置特定的已知值,并使用[self.view viewWithTag:TheTagForTheLabel];
访问它们
Two approaches I can think of:
Add all your labels to an
NSArray
;Set the
tag
property for all the labels with a specific know value for each label and access them with[self.view viewWithTag:TheTagForTheLabel];
我可能会创建一个 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.