开发者

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 b开发者_C百科uttonWithType: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;

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.

not able to change property of objects of NSArray

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. 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];. 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:

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:

for (UIButton * button in buttonsArray) 
{
    button.alpha = 0;
    // etc... 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜