我想以编程方式创建10个按钮。每个按钮都有一个带有两个标签的子视图(一个字符串和一个整数)。
我在循环中创建了这10个按钮后,我想访问这两个标签。我尝试使用标签0来对NSLog标签进行操作,但它没有用。
所有这些都感觉有点笨拙,所以如果我无处可去,请纠正我:
for (int i = 0; i < 9; i++) {
UIButton* btn = [[[UIButton alloc] initWithFrame:CGRectMake(55, i*(indexHeight+indexSpacing), indexWidth, indexHeight)] autorelease];
btn.tag = i;
[btn setBackgroundImage:nil forState:UIControlStateNormal];
[btn addTarget:self
action:@selector(buttonTapped:)
forControlEvents:UIControlEventTouchUpInside];
// LABELS
UILabel *btnTitle = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, indexWidth, indexHeight)] autorelease];
btnTitle.text = @"Empty";
UILabel *pageTitle = [[[UILabel alloc] initWithFrame:CGRectMake(190, 0, 30, indexHeight)] autorelease];
pageTitle.text = @"x";
[indexView addSubview:btn];
[btn addSubview:btnTitle];
[btn addSubview:pageTitle];
}
这是我尝试过的,问题出在哪里:
NSLog (@"Accessing label 1 in button with tag 0 in indexView: '%@'", [[indexView viewWithTag:0] btnTitle.text]);
NSLog (@"Accessing label 2 in button with tag 0 in indexView: '%@'", [[indexView viewWithTag:0] pageTitle.text]);
“UILabel created programmatically - find it again?”是我能找到的最接近的问题,但它并没有真正回答如何在btn的子视图中访问标签的问题。
答案 0 :(得分:2)
您也可以尝试为每个标签分配一个标签,第一个标签为100,第二个标签为101,然后您的代码看起来像这样,用于检索标签
NSLog (@"Accessing label 1 in button with tag 0 in indexView: '%@'", [[indexView viewWithTag:0] viewWithTag:100].text);
NSLog (@"Accessing label 2 in button with tag 0 in indexView: '%@'", [[indexView viewWithTag:0] viewWithTag:101].text);
答案 1 :(得分:1)
如果设置了indexHeight,indexSpacing,indexWidth,则以下内容肯定有效。
for (int i = 0; i < 9; i++) {
UIButton* btn = [[[UIButton alloc] initWithFrame:CGRectMake(55, i*(indexHeight+indexSpacing), indexWidth, indexHeight)] autorelease];
btn.tag = i;
[btn setBackgroundImage:nil forState:UIControlStateNormal];
[btn addTarget:self
action:@selector(buttonTapped:)
forControlEvents:UIControlEventTouchUpInside];
// LABELS
UILabel *btnTitle = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, indexWidth, indexHeight)] autorelease];
btnTitle.text = @"Empty";
btnTitle.tag = i+100;
UILabel *pageTitle = [[[UILabel alloc] initWithFrame:CGRectMake(190, 0, 30, indexHeight)] autorelease];
pageTitle.text = @"x";
pageTitle.tag = i+101;
[indexView addSubview:btn];
[btn addSubview:btnTitle];
[btn addSubview:pageTitle];
}
for (int i = 0; i < 9; i++) {
UILabel *btnTitle = (UILabel *)[[indexView viewWithTag:i] viewWithTag:100+i];
UILabel *pageTitle = (UILabel *)[[indexView viewWithTag:i] viewWithTag:101+i];
NSLog (@"btnTitle.text: '%@'", btnTitle.text);
NSLog (@"pageTitle.text: '%@'", pageTitle.text);
}
答案 2 :(得分:1)
在头文件中添加此ivar:
NSMutableArray *buttonArray;
在.m文件的init中初始化此数组:
buttonArray = [NSMutableArray array];
[buttonArray retain];
按如下方式更改for循环:
for (int i = 0; i < 9; i++)
{
UIButton* btn = [[[UIButton alloc] initWithFrame:CGRectMake(55, i*(indexHeight+indexSpacing), indexWidth, indexHeight)] autorelease];
btn.tag = i;
[btn setBackgroundImage:nil forState:UIControlStateNormal];
[btn addTarget:self
action:@selector(buttonTapped:)
forControlEvents:UIControlEventTouchUpInside];
// LABELS
UILabel *btnTitle = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, indexWidth, indexHeight)] autorelease];
btnTitle.text = @"Empty";
UILabel *pageTitle = [[[UILabel alloc] initWithFrame:CGRectMake(190, 0, 30, indexHeight)] autorelease];
pageTitle.text = @"x";
[indexView addSubview:btn];
[btn addSubview:btnTitle];
[btnTitle release]; //avoiding leaks
[btn addSubview:pageTitle];
[pageTitle release]; //avoiding leaks
//The changes are here.....
[buttonArray addObject:btn];
[btn release];
}
要从数组执行按钮(例如按钮5):
UIButton *thisButton = (UIButton *)[buttonArray objectAtIndex:5];
使用此按钮执行任何操作:)
PS: 你在for循环中分配那些btn对象(看起来它们不是ivars)。 一旦你在for循环之外,它们将失去范围,所以你无论如何都无法访问它们。 上面使用NSMutableArray的方法也解决了这个问题,因为现在你已经创建了btn并将它们添加到一个ivar数组中,因此你不会在for循环之外丢失范围。 此外,如果要在其上设置自定义子视图,则应该将UIButton子类化。