2011-08-12 20 views
0

的对象的性质我有一个:无法更改的NSArray

@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中创建所有按钮并创建连接。

enter image description here

我张贴的图片,这样就可以得到一个想法......

反正是获得点击一个按钮时的执行方法是:

-(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++; 
    } 
} 

我以前添加的按钮根本不会改变。阿尔法也不会改变。当我运行最后一个方法时,我不知道我在更改哪个按钮。

回答

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]; 
} 

这也避免了填充硬编码的阵列,更灵活,更不容易出错代码。

+0

非常感谢!那让我很难过。 –

+0

哦,我忘了提及你必须使用'NSMutableArray'实例才能够添加对象。然后你可以像下面这样初始化'buttonsArray':'buttonsArray = [[NSMutableArray alloc] init];' – Joost

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]; 
} 

的问题之一,就是你设置标签上的按钮,然后更换下一行的按钮实例。

0

这看起来可疑,你做两次:

for (UIButton *button in buttonsArray) { 

    button = [buttonsArray objectAtIndex:counter]; 

您不应该修改循环内的循环枚举变量按钮。你只需使用按钮就可以了。

督察,你要么做:

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...