1

我正在尝试编写一个简单的游戏,该游戏基于从几个问题(如学校考试)中选择正确答案(AB 或 C),并计算正确/错误答案以设置分数。我不知道如何使用 NSArray 存储所有问题和正确答案以及如何设置分数。我唯一的解决方案是为每个问题创建一个新视图,但这听起来像是一个糟糕的解决方案。任何帮助将不胜感激。谢谢

视图控制器.h

@interface ViewController : UIViewController {

IBOutlet UILabel *numPregunta;
IBOutlet UILabel *pregunta;
IBOutlet UILabel *lblRespuesta1;
IBOutlet UILabel *lblRespuesta2;
IBOutlet UILabel *lblRespuesta3;
IBOutlet UILabel *lblResultado;

IBOutlet UIButton *respuesta1;
IBOutlet UIButton *respuesta2;
IBOutlet UIButton *respuesta3;

}

-(IBAction)btnRespuesta1:(id)sender;
-(IBAction)btnRespuesta2:(id)sender;
-(IBAction)btnRespuesta3:(id)sender;
-(IBAction)nextPregunta:(id)sender;

@end

视图控制器.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController



- (void)viewDidLoad
{

numPregunta.text = @"Question 1";
pregunta.text = @"Question 1 text";
lblRespuesta1.text = @"ANSWER 1";
lblRespuesta2.text = @"ANSWER 2";
lblRespuesta3.text = @"ANSWER 3 ";


[super viewDidLoad];


}

-(IBAction)btnRespuesta1:(id)sender

{
    if(pregunta.text == @"Question 1 text")
{

    lblRespuesta1.textColor = [UIColor redColor];
    lblResultado.text = @"False! ";
    lblResultado.textColor = [UIColor redColor];

}
}


-(IBAction)btnRespuesta2:(id)sender

{
    if(pregunta.text == @"Question 1 text")
    {

        lblRespuesta2.textColor = [UIColor redColor];
        lblResultado.text = @"False! ";
        lblResultado.textColor = [UIColor redColor];

    }

}

-(IBAction)btnRespuesta3:(id)sender

{
if(pregunta.text == @"Question 1 text")
{

    lblRespuesta3.textColor = [UIColor greenColor];
    lblResultado.text = @"Nice! ";
    lblResultado.textColor = [UIColor greenColor];



}

}    



- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

@end
4

2 回答 2

0

您选择的方式是漫长的过程最初您必须将所有每个问题、选项和答案保存在字典中,并将字典添加到数组中,这样使用起来很简单。

[dictionary setObject:value forKey:@"question"];
[dictionary setObject:value forKey:@"option1"];
[dictionary setObject:value forKey:@"option2"];
[dictionary setObject:value forKey:@"option3"];
[dictionary setObject:value forKey:@"option4"];
[dictionary setObject:value forKey:@"answer"];

并将其添加到数组

[array addObject dictionary]

pregunta.text = [[array objectAtIndex:1]objectForKey:"question"];
lblRespuesta1.text = [[array objectAtIndex:1]objectForKey:"option1"];
lblRespuesta2.text = [[array objectAtIndex:1]objectForKey:"option2"];
lblRespuesta3.text = [[array objectAtIndex:1]objectForKey:"option3"];
于 2012-06-18T12:37:22.903 回答
0

你可以有一个问题数组,说 questionsArray,它将包含所有问题。第二次创建字典数组,它将包含所有选项和正确的结果说 resultArray 包含在 0 索引字典,键为 Answer1、Answer2、Answer3,结果和值将包含相应的values.现在取一个全局变量,它将存储问题编号意味着它正在显示哪个问题,比如 question0、question1..etc,这只是 questionsArray 中的问题索引。假设您在索引 0 处显示问题,然后显示相应的选项到那个索引,即 0 如下:

NSString *answer1 = [[resultArray objectAtIndex:0] valueForKey:@"Answer1"];
NSString *answer2 = [[resultArray objectAtIndex:0] valueForKey:@"Answer2"];
NSString *answer3 = [[resultArray objectAtIndex:0] valueForKey:@"Answer3"];

并说正确的答案是 answer2,然后结果键将为 2。并为每个按钮提供标签,如 1,2 和 3。然后在按钮操作中,您可以检查用户是否选择了正确的选项,如下所示:

if(btn.tag == [[resultArray objectAtIndex:0] valueForKey:@"result"])
{
   //correct answer is selected so increment score.
}
于 2012-06-18T12:44:54.870 回答