开发者

how to find which objects are selected:TRUE from 64 buttons labelled b1, b2 etc

开发者 https://www.devze.com 2023-01-16 18:11 出处:网络
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 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?


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


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

}


Try [b setAlpha:0.0];


Set the tag when creating the buttons and store the tag index in an array.

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

0

精彩评论

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