[英]not able to change property of objects of NSArray
I have a:我有一个:
@property(nonatomic, retain) NSArray * buttonsArray;
...
...
@synthesize buttonsArray;
when the view loads I initialize it as:当视图加载时,我将其初始化为:
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];
// this code places the buttons from buttons array on top of the images in my view. // 此代码将按钮数组中的按钮放置在我视图中的图像顶部。 I placed those images in an array called imagesArrayV;
我将这些图像放在一个名为 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++;
}
the purpose of this is to save time creating all the buttons in xcode and creating the connections.这样做的目的是节省在 xcode 中创建所有按钮并创建连接的时间。
I posted a picture so that you can get an idea...我发了一张图片,让你有一个想法……
Anyways the method that get's executed when clicking a button is:无论如何,单击按钮时执行的方法是:
-(void) test: (id) sender{
UIButton*btn = (UIButton*)(sender);
int tagnumber = [btn tag];
NSLog(@"%i",tagnumber);
}
why is it that when I press on the buttons the tag is equal to 0 when I set it to something else ( look for: // ********* ) when creating the button.为什么当我按下按钮时标签等于0,而我在创建按钮时将其设置为其他值(查找:// *********)。 Moreover when I run this method:
此外,当我运行此方法时:
-(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++;
}
}
the buttons that I previously added do not change at all.我之前添加的按钮根本没有改变。 also the alpha does not change.
阿尔法也不会改变。 I don't know what button's I am changing when I run the last method.
当我运行最后一个方法时,我不知道我正在更改哪个按钮。
Just below the line where you set the tag, you have the line button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
就在您设置标签的行下方,您有一行
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
. . This overwrites the button obviously.
这显然会覆盖按钮。 The action is then added to the newly created button, the buttons in the array are left unaltered.
然后将操作添加到新创建的按钮,数组中的按钮保持不变。
Personally I would rewrite the code as follows:我个人会重写代码如下:
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];
}
This also avoids populating the array hardcoded, for more flexible, less error-prone code.这也避免了填充硬编码的数组,以获得更灵活、更不容易出错的代码。
try using this code instead of the third section above:尝试使用此代码而不是上面的第三部分:
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];
}
one of the issues, is that you were setting the tag on button, then replacing the button instance on the next line.其中一个问题是您在按钮上设置标签,然后在下一行替换按钮实例。
This looks suspicious, and you do it twice:这看起来很可疑,你做了两次:
for (UIButton *button in buttonsArray) {
button = [buttonsArray objectAtIndex:counter];
You should not modify the loop enumeration variable button inside the loop.您不应修改循环内的循环枚举变量按钮。 You simply use button as it is.
您只需按原样使用按钮。
IOW, you either do: IOW,你要么这样做:
for (counter = 0; counter < buttonsArray.count; counter++)
{
UIButton *button = [buttonsArray objectAtIndex: counter];
button.alpha = 0;
// etc...
or you simply get rid of counter
and do:或者您只需摆脱
counter
并执行以下操作:
for (UIButton * button in buttonsArray)
{
button.alpha = 0;
// etc...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.