我有一个问题视图,将显示4个答案,只有1个是正确的。
但我不希望同一个按钮始终是正确答案。
我想知道如何随机放置4个UIButton并且每次都有价值。
当用户再次回答此问题时,答案将出现在不同的按钮
中
我放置X,y,W,H
按钮1 5,219,230,45
按钮2 5,273,230,45
按钮3 244,224,230,45
按钮4 244,273,230,45
答案 0 :(得分: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;
}
}
}
}
答案 1 :(得分: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];
答案 2 :(得分:1)
您可以始终在相同位置创建按钮,并随机按钮上的标签。如果使用数组来提供val1,val2,val3等标签,则可以随机化数组。
答案 3 :(得分:1)
我建议不要改变按钮的位置,而是你可以做的是随机改变按钮的currentTitle
属性,一旦用户点击按钮;你可以得到按钮的currentTitle
并检查它是否是正确的答案。
答案 4 :(得分:1)
在4个选项中,只有一个是正确的,所以为什么你担心,只需给你的UIButton标记。在touchUpInside事件或您在其上指定的任何操作。只需将正确的答案标记与用户触摸的按钮标记匹配即可。
您应该设置一些逻辑来随机设置不同按钮标记的答案。
答案 5 :(得分: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 )];
答案 6 :(得分: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];
}
}
答案 7 :(得分:0)
使用这4个按钮创建自己的类。在这个课程中,您可以存储按钮的位置。然后你可以添加一个成员函数(如下所示),它将绘制它们:
-(void)drawButtonsWithIncorrectAtIndex:(int)index