2

I have to hide one of mine four UIButtons, randomly selected, BUT Excepting one.

for this, I created a NSMutableArray, and added all button there, as following example:

rand_btns = [[NSMutableArray alloc] initWithObjects: _bt1, _bt2, _bt3, _bt4,nil];

No, each button has its own tag: _bt1 has tag 1, _bt2 has tag 2, and so...

Please, any ideas? I want to hide one random button, but excepting a button which has tag equal to my: int Level.

I want to use this for a Quiz App.

So, my int Level is from 1-4 random number, when one of mine four buttons has tag equal to mine int Level, that button should be excepted from hiding.

4

4 回答 4

0

Just do this.

int randomTag = rand() % 4;
while (randomTag == Level) {
    randomTag = rand() % 4;
}
[[randButtons objectAtIndex:randomTag] setHidden:YES] 
于 2013-08-20T03:30:09.077 回答
0

Try this

-(void)randomSelForLevel:(NSInteger)level
{
    int randomTag = rand() % 4;
    while (randomTag == level) {
        randomTag = rand() % 4;
    }

    for (int i=0; i<[rand_btns count]; i++) {
        [[rand_btns objectAtIndex:randomTag] setHidden:NO];
    }
    [[rand_btns objectAtIndex:randomTag] setHidden:YES];
}
于 2013-08-20T06:38:41.870 回答
0

to select random no between two no use this code:
int random = lowno + arc4random() % (highno-lowno);
Thanks.

于 2013-11-26T07:29:51.853 回答
0
abarr = [[NSMutableArray alloc]init];

 for (int i = 0; i < 5; i++){
    ab = [[UIButton alloc]init];

    ab = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    ab.tag = i;

   [ab setTitle:[NSString stringWithFormat:@"%ld",ab.tag] forState:UIControlStateNormal];

    [ab addTarget:self action:@selector(clicked:) forControlEvents:UIControlEventTouchUpInside];

    [ab sizeToFit];

    ab.backgroundColor = [UIColor yellowColor];

    [abarr addObject:ab];

    [self.view addSubview:ab];

    switch (ab.tag) {

        case 0:
            ab.frame=CGRectMake(0, 0, 50,50);
            break;

        case 1:
            ab.frame=CGRectMake(50, 0, 50,50);
            break;

        case 2:
            ab.frame=CGRectMake(100, 0, 50,50);
            break;

        case 3:
            ab.frame=CGRectMake(150, 0, 50,50);
            break;

        default:
            break;
    }


}

randomTag = rand() % 4;

for (int i=0; i<[abarr count]; i++) {
    [[abarr objectAtIndex:randomTag] setBackgroundColor:[UIColor redColor]];

}
[[abarr objectAtIndex:randomTag] setBackgroundColor:[UIColor redColor]];
    }

-(void)clicked:(UIButton*)button
 {

  NSLog(@"%ld",(long int)[button tag]);

     for (int i=0; i<[abarr count]; i++) 
 {
     [[abarr objectAtIndex:randomTag] setHidden:YES];
    }


 }
于 2015-07-17T13:47:40.510 回答