2012-03-15 6 views
0

質問ビューがあり、4つの回答が表示されますが、1つしか正しくありません。UIButtonと値をランダムに配置する方法

しかし、私は同じボタンを常に正しい答えにしたくありません。

私は、ランダムな4 UIButtonの配置と値ごとに行うのですか知りたいだろう。ユーザーが再びこの質問に行くとき

enter image description here

答えはX、Y、W、H

ボタン1 5219230の私の配置ボタン

enter image description here

を異なるになります、45

ボタン2 5,273,230,45

ボタン3 244,224,230,45

ボタン4 244,273,230,45

+1

ボタンごとにタグ(1,2,3,4)を設定することができます。ランダム機能を使用してタグを検索し、そのボタンの「正しい」テキストを設定します。 –

答えて

2

私は同じようなゲームアプリで作業していたので、間違いなく役立ついくつかのコードを手に入れました。下にそれをチェックしてください。正しい、そして間違ったボタンのクリックのために、あなたが 'correctAnswerMethod'と 'wrongAnswerMethod'を呼び出すことにそれを入れました。

// 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

ありがとうMadhumal :) – Desmond

1

あなたは常に同じ位置にボタンを作成して、ボタンのラベルをランダム化することができます。配列を使用してval1、val2、val3などのラベルを提供する場合は、配列をランダム化できます。

1

私は何を行うことができますが、ランダムに、ユーザーがボタンをクリックすると、ボタンのcurrentTitleプロパティを変更しているむしろボタンの位置を変更しないことをお勧め。ボタンのcurrentTitleを取得し、正解かどうかを確認することができます。

1

4つのオプションのうち1つだけが正しいので、心配する必要はありません。あなたのUIButtonにタグを付けてください。 touchUpInsideイベントまたは指定したアクション。正解タグとユーザーがタッチしたボタンタグを一致させるだけです。

あなたはランダムに別のボタンタグと答えを設定するには、いくつかのロジックを設定する必要があります。

+1

Wah Hemangsaheb ....偉大な答え...から:Krunal、 'C'div。、Bvn ... – Krunal

+0

@ゴティ、クルナールありがとう!ここにお会いできてよかった! :) – Hemang

1

あなたはボタンを作成し、subViewとしてそれらを追加する必要があります。ランダムな場所にボタンを配置する必要がある場合は、以下を使用できます。

[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)]; 
1

ボタンのxとyをここで設定する必要はありません。ただ、タグ1と言う、あなたのボタンのタグを設定する - 4は、乱数を生成1 - 4(あなたのボタンの可能性タグ)、NSArrayのにあなたのボタンを保存するなどのループを実行します。独自のクラスを作成し

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

をこれらの4つのボタンで。このクラスでは、ボタンの位置を保存できます。私は私の側で同じのためのコードを実装しているし、それが正常に動作します

-(void)drawButtonsWithIncorrectAtIndex:(int)index 
3

:あなたはそれらを描画しますメンバ関数(以下のようなもの)を追加することができます。

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

はうまく動作します。おかげで – Sujal

関連する問題