views:

51

answers:

4

Hi, I have a project where there are 64 buttons, you have to click certain ones, then click 'done'. If you clicked the right ones, you get some points, and those then need to disappear. I have let the user keep track of which buttons are pressed by doing sender setSelected:TRUE. The first bit is all working fine, but I'd like to be able to then hide the buttons that were selected when the user clicked 'done'.

My current thoughts are: - what I used in Actionscript to do the same thing was

for (i=1;i++;i<65) {
if(getProperty ("b"+ i, _visible) == true) {do blah blah blah} }

I'm really really really hoping there is an obvious equivalent in objective C that does the same thing?

I definitely do not want to have to go through all 64 buttons and type if ([b1 isSelected == TRUE]etc...
I can't just use sender, as it may be several buttons that have previously been selected that I need to access.

EDIT - This is now the code that is called when user presses one of the 64 buttons.

        -(IBAction) pressed:(id)sender {
        UIButton *button = (UIButton *)sender;
        if ([sender isSelected] ==FALSE) {  
            [sender setSelected:TRUE];  
        }
        else {
            [sender setSelected:FALSE];
        }
            if ([myArray containsObject:sender])
            {
                [myArray removeObject:sender];
            }
            else {
                [myArray addObject:sender];
            }
    }

This is called when they press the 'done' button.

-(IBAction) checkTotal:(id)sender {

if (total == [(totaltxt.text) intValue]) {
    score += 1;
    scoretxt.text = [NSString stringWithFormat:@"%i", score];
    for (UIButton *b in myArray)
    {
        [b setHidden:TRUE];
    }
    [myArray removeAllObjects];
    }

else {
    // some indication that they pressed the button but were wrong.
}

}

It unfortunately still won't hide the button.
It works if I change it to [n1 setHidden:TRUE] to hide the matching textbox above the button, but won't hide even a specific button -eg- [b1 setHidden:TRUE], let alone all the buttons in my array. AAAAAAAARGH!!!! Any ideas?

A: 

If you dont want to iterate through all the buttons then how about store a reference to your button in an array then just iterate through that array and clear it when the use clicks done?

    -(void)click:(id)sender
    {
       if([myArray containsObject:sender])
           [myArray removeObject:sender];
       else
       [myArray addObject:sender];

    } 

    -(void)doneClicked:(id)sender
    {
       for(UIButton *b in myArray)
    {
       [b setHidden:TRUE];
    }
    [myArray removeAllObjects]; //whateverr the method is i dont remember it off the top of my head

}
Daniel
Will [myArray deleteObject:sender] work if they unselect the button?
Emily Hughes
it's not necessarily the last object that needs to be deleted
Emily Hughes
yes it will...check out my edit
Daniel
ok, tried it and it's not liking it... Weirdly, it will hide the textbox above button b1, using [n1 setHidden:TRUE], but won't hide the button using [b1 setHidden:TRUE] ! Is there something different I should do to hide buttons?
Emily Hughes
setHidden should work for buttons...not sure what you are doing but something is not right..
Daniel
have posted new code - see edit. Have also tried swapping hidden for .enabled, but that won't work either.
Emily Hughes
DUH!!! just checked, and I'd not yet set b1 as a referencing outlet as I was previously using the tab of the button to access it!
Emily Hughes
I can now hide specific buttons, but it still won't do it for all *b in myArray.
Emily Hughes
have u initialized myArraY??
Daniel
have tried - NSMutableArray *myArray = [[NSMutableArray alloc] init];
Emily Hughes
but it won't let me do it - says my initialiser element is not a constant. This is why I was hoping to avoid an array! I'm new to Objective C and my brain is letting me down!!
Emily Hughes
i mean why would you want to avoid arrays? even if you are new to objective-c...post the code you are using i can help you out
Daniel
Thank you for trying, but I think I need a little more practice with arrays - not something I'm comfortable using yet! Makes it tricky to troubleshoot... You've been really helpful though :)
Emily Hughes
u should probably read up on some objective-c primer books...arrays are basic and are not difficult to use (if you know how to use arrays in any other programming enviroment)
Daniel
yeah, I'm working on it - they're not something I've ever used in my other programming experience - I'm strictly an amateur...!
Emily Hughes
A: 

Try [b setAlpha:0.0];

Martha
A: 

set Tag when create the Buttons

and store the tag index in array in function

-(IBAction) pressed:(id)sender {

}

by sender.tag

then finally run the loop hide all object and again run loop to unhide the object if it exists in array.

Ashish Mathur
A: 

You can iterate on your view's subviews, and check whether the subview is a button:

for (UIView *view in self.view.subviews)
{
    if ([view isKindOfClass: [UIButton class]])
    {
        // Do processing here
        // For instance:
        if ((UIButton *)view).isSelected)
            view.hidden = YES;
    }
}
jv42
i love you!!!!!!!! thank you :) works beautifully.
Emily Hughes
Glad to have been helpful ;)
jv42