我有一个:
@property(nonatomic, retain) NSArray * buttonsArray;
...
...
@synthesize buttonsArray;
当视图加载时我将其初始化为:
buttonsArray = [[NSArray alloc] initWithObjects:
[UIButton buttonWithType:UIButtonTypeRoundedRect],
[UIButton buttonWithType:UIButtonTypeRoundedRect],
[UIButton buttonWithType:UIButtonTypeRoundedRect],
[UIButton buttonWithType:UIButtonTypeRoundedRect],
[UIButton buttonWithType:UIButtonTypeRoundedRect],
[UIButton buttonWithType:UIButtonTypeRoundedRect],
[UIButton buttonWithType:UIButtonTypeRoundedRect],
[UIButton buttonWithType:UIButtonTypeRoundedRect],
[UIButton buttonWithType:UIButtonTypeRoundedRect],
[UIButton buttonWithType:UIButtonTypeRoundedRect],
[UIButton buttonWithType:UIButtonTypeRoundedRect],
[UIButton buttonWithType:UIButtonTypeRoundedRect],
[UIButton buttonWithType:UIButtonTypeRoundedRect],
[UIButton buttonWithType:UIButtonTypeRoundedRect],
[UIButton buttonWithType:UIButtonTypeRoundedRect],
[UIButton buttonWithType:UIButtonTypeRoundedRect],
[UIButton buttonWithType:UIButtonTypeRoundedRect],
[UIButton buttonWithType:UIButtonTypeRoundedRect],
[UIButton buttonWithType:UIButtonTypeRoundedRect],
[UIButton buttonWithType:UIButtonTypeRoundedRect],
[UIButton buttonWithType:UIButtonTypeRoundedRect],
nil];
//此代码将按钮数组中的按钮放在我视图中的图像顶部。我将这些图像放在一个名为imagesArrayV;
的数组中int counter = 0;
counter=0;
for (UIButton *button in buttonsArray) {
button = [buttonsArray objectAtIndex:counter];
[button setTag:counter]; // *********
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:@selector(test:) forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Hello" forState:UIControlStateNormal];
UIImageView *tempImage = [imagesArrayV objectAtIndex:counter];
CGRect tempRect = tempImage.frame;
button.frame = tempRect;
[self.ViewMainV addSubview:button];
counter++;
}
这样做的目的是节省在xcode中创建所有按钮并创建连接的时间。
我发布了一张图片,以便您可以了解...
无论如何,单击按钮时执行的方法是:
-(void) test: (id) sender{
UIButton*btn = (UIButton*)(sender);
int tagnumber = [btn tag];
NSLog(@"%i",tagnumber);
}
当我按下按钮时,当我在创建按钮时将其设置为其他内容(查找:// *********)时标签等于0,为什么呢?而且当我运行这个方法时:
-(void) someOtherMethod{
int counter = 0;
for (UIButton *button in buttonsArray) {
button = [buttonsArray objectAtIndex:counter];
button.alpha = 0;
button.titleLabel.text = @"I change the title";
counter++;
}
}
我之前添加的按钮根本没有变化。 alpha也不会改变。当我运行最后一个方法时,我不知道我正在改变什么按钮。
答案 0 :(得分:4)
在您设置代码的行下方,您有一行button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
。这显然会覆盖按钮。然后将操作添加到新创建的按钮中,阵列中的按钮保持不变。
就个人而言,我会按如下方式重写代码:
for (int counter = 0; counter < numberOfButtons; ++counter) {
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTag:counter];
[button addTarget:self action:@selector(test:) forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Hello" forState:UIControlStateNormal];
UIImageView *tempImage = [imagesArrayV objectAtIndex:counter];
CGRect tempRect = tempImage.frame;
button.frame = tempRect;
[self.ViewMainV addSubview:button];
[buttonsArray addObject:button];
}
这也避免了对数组进行硬编码填充,以获得更灵活,更不容易出错的代码。
答案 1 :(得分:0)
尝试使用此代码而不是上面的第三部分:
for(int i=0;i<[buttonsArray count];i++){
UIButton *button=[buttonsArray objectAtIndex:i];
[button addTarget:self action:@selector(test:) forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Hello" forState:UIControlStateNormal];
UIImageView *tempImage = [imagesArrayV objectAtIndex:counter];
button.frame = tempImage.frame;
button.tag =i;
[self.ViewMainV addSubview:button];
}
其中一个问题是,您是在按钮上设置标记,然后替换下一行的按钮实例。
答案 2 :(得分:0)
这看起来很可疑,你做了两次:
for (UIButton *button in buttonsArray) {
button = [buttonsArray objectAtIndex:counter];
您不应该修改循环内的循环枚举变量按钮。你只需按原样使用按钮即可。
IOW,你要么:
for (counter = 0; counter < buttonsArray.count; counter++)
{
UIButton *button = [buttonsArray objectAtIndex: counter];
button.alpha = 0;
// etc...
或者您只是摆脱counter
并执行:
for (UIButton * button in buttonsArray)
{
button.alpha = 0;
// etc...