how to random placement of UIButton and value

前端 未结 8 1265
南方客
南方客 2021-01-28 22:48

i have a question view and will show 4 answers, only 1 are correct.

but i do not want the same button to be always the correct answer.

i will like to know how do

8条回答
  •  别那么骄傲
    2021-01-28 23:04

    i dont think you need to set your button's x and y here. just set the tag of your buttons, say tag 1 - 4, generate random number 1 - 4 (possible tag of your buttons), save your buttons to NSArray and do a loop like:

    NSArray *buttonsArr = [[NSArray alloc]initWithObjects:button1,button2, button3, button4, nil];
    int randomInt = (arc4random() % 4) + 1;
    for (int i = 0; i < [buttonsArr count]; i++) {
        if ([(UIButton*)[buttonsArr objectAtIndex:i] tag] == randomInt) {
            [[buttonsArr objectAtIndex:i] setTitle:@"correct" forState:UIControlStateNormal];
        }else{
            [[buttonsArr objectAtIndex:i] setTitle:@"wrong" forState:UIControlStateNormal];
        }
    }
    

提交回复
热议问题