how to random placement of UIButton and value

前端 未结 8 1264
南方客
南方客 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 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;
                }
    
            }
    
        }
    
    }
    
    0 讨论(0)
  • 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];
        }
    }
    
    0 讨论(0)
  • 2021-01-28 23:08

    You can create the buttons in same position always and randomize the labels on the button. If you are using an array to provide labels like val1, val2, val3 etc, you can randomize the array.

    0 讨论(0)
  • 2021-01-28 23:12

    I happened to be working on a similar game app, so got some code which will definitely help. Check it out below. Note that I've put it in that you'd call a 'correctAnswerMethod' and a 'wrongAnswerMethod' for the correct and incorrect button clicks.

    // Define the four buttons in their respective frames, create the first button as the correct one, we'll shuffle it later.
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button1 setTitle:@"Correct Answer" forState:UIControlStateNormal];
    [button1 addTarget:self action:@selector(correctAnswerMethod:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button1];
    
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button2 setTitle:@"Wrong Answer 1" forState:UIControlStateNormal];
    [button2 addTarget:self action:@selector(wrongAnswerMethod:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button2];
    
    UIButton *button3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button3 setTitle:@"Wrong Answer 2" forState:UIControlStateNormal];
    [button3 addTarget:self action:@selector(wrongAnswerMethod:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button3];
    
    UIButton *button4 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button4 setTitle:@"Wrong Answer 3" forState:UIControlStateNormal];
    [button4 addTarget:self action:@selector(wrongAnswerMethod:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button4];
    
    
    //Create an array with the rectangles used for the frames
    NSMutableArray *indexArray = [NSMutableArray arrayWithObjects:
                                  [NSValue valueWithCGRect:CGRectMake(5, 219, 230, 45)], 
                                  [NSValue valueWithCGRect:CGRectMake(5, 273, 230, 45)], 
                                  [NSValue valueWithCGRect:CGRectMake(244, 219, 230, 45)], 
                                  [NSValue valueWithCGRect:CGRectMake(244, 273, 230, 45)], nil];
    
    //Randomize the array
    NSUInteger count = [indexArray count];
    for (NSUInteger i = 0; i < count; ++i) {
        int nElements = count - i;
        int n = (arc4random() % nElements) + i;
        [indexArray exchangeObjectAtIndex:i withObjectAtIndex:n];
    }
    
    //Assign the frames
    button1.frame = [((NSValue *)[indexArray objectAtIndex:0]) CGRectValue];
    button2.frame = [((NSValue *)[indexArray objectAtIndex:1]) CGRectValue];
    button3.frame = [((NSValue *)[indexArray objectAtIndex:2]) CGRectValue];
    button4.frame = [((NSValue *)[indexArray objectAtIndex:3]) CGRectValue];
    
    0 讨论(0)
  • 2021-01-28 23:12

    You should create button and add them as subView. When you need to put button at random location, you can use:

    [button1 setFrame:CGRectMake(5,219,230,45)];
    [button2 setFrame:CGRectMake(5,273,230,45)];
    [button3 setFrame:CGRectMake(244,224,230,45)];
    [button4 setFrame:CGRectMake(244,273,230,45 )];
    
    0 讨论(0)
  • 2021-01-28 23:20

    Out of 4 options only one is correct so why you worry, just give tag to your UIButton. In touchUpInside event or any action you've specified on it. Just match the correct answer tag with user touched button tag.

    You should set some logic to set answer with different button tag randomly.

    0 讨论(0)
提交回复
热议问题