how to random placement of UIButton and value

前端 未结 8 1267
南方客
南方客 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条回答
  •  -上瘾入骨i
    2021-01-28 23:04

    I have implemented code for same at my side and it works fine.

    -(void)randomAllocation
    {
        NSMutableArray* allstring = [[NSMutableArray alloc]initWithObjects:@"Correct",@"Wrong",@"Wrong1",@"Wrong2", nil];
    
        NSMutableArray* outcomeOFrandom = [[NSMutableArray alloc]init];
    
        for (int i=0; i<[allstring count]; i++) {
            int temp = 0;
    
            while (temp==0) {
    
                int randomInt  =  arc4random()  % 4;
    
    
                BOOL result = [outcomeOFrandom containsObject:[allstring objectAtIndex:randomInt]];
                if (result==FALSE ) {
    
                    UIButton* btn = [[UIButton alloc]init];
                    [btn setTitle:[allstring objectAtIndex:randomInt] forState:UIControlStateNormal];
                    [btn setBackgroundColor:[UIColor blackColor]];
    
                    NSLog(@"tital=%@",[allstring objectAtIndex:randomInt]);
    
                    if (i==0) {
                        [btn setFrame:CGRectMake(5,219,230,45)];
                    }
                    else if(i==1) 
                        [btn setFrame:CGRectMake(5,273,230,45)];
                    }
                    else if(i==2) {
                        [btn setFrame:CGRectMake(244,224,230,45)];
                    }
                    else if(i==3) {
                        [btn setFrame:CGRectMake(244,273,230,45)];
                    }
    
                    [outcomeOFrandom addObject:[allstring objectAtIndex:randomInt]];
                    [self.view addSubview:btn];
                    [btn release];
    
                    temp=1;
                    break;
                }
    
            }
    
        }
    
    }
    

提交回复
热议问题