将一个数组值存储为变量的语法

时间:2014-03-01 11:48:15

标签: ios arrays sqlite

在设计测验应用程序时,我想将用户选择的答案与sqlite数组中的正确答案进行比较。

为此,我想在viewDidLoad:上执行以下操作,加载正确答案(1,2,3等整数)并将其存储为qACorrect变量。< / p>

然后在- (IBAction)submit:(id)sender上将变量qACorrectqASelected进行比较

有人可以帮我解释将数组中导入的qACorrect的值分配给新变量qACorrect的语法吗?我在ViewDidLoad上使用此代码的其余部分来加载问题:

- (void)viewDidLoad
{
    MySjtQuestionslist * MySJT =[[MySjtQuestionslist alloc] init];
    self.SJT = [MySJT getMySjtQuestion];
    [self.QuestionText setText:((SjtQuestionlist *) [self.SJT objectAtIndex:0]).SJTQuestion];
    [self.labelA setText:((SjtQuestionlist *) [self.SJT objectAtIndex:0]).SJTQuestionA];
    [self.labelB setText:((SjtQuestionlist *) [self.SJT objectAtIndex:0]).SJTQuestionB];
    [self.labelC setText:((SjtQuestionlist *) [self.SJT objectAtIndex:0]).SJTQuestionC];
    [self.labelD setText:((SjtQuestionlist *) [self.SJT objectAtIndex:0]).SJTQuestionD];
    [self.labelE setText:((SjtQuestionlist *) [self.SJT objectAtIndex:0]).SJTQuestionE];

    [super viewDidLoad];
    [scroller setScrollEnabled:YES]; //scrollview stuff
    [scroller setContentSize:CGSizeMake(320, 1000)]; //scrollview stuff
}

1 个答案:

答案 0 :(得分:0)

基于 qAVariable是从分段控制器中选择的值的假设。&amp; qACorrect属于NSString类型

您无需实施额外的“提交”方法。在段值更改时,您可以执行此操作。

- (IBAction)onSegmentValueChanged:(UISegmentedControl *)sender 
{
     NSString *selAnswer= [sender titleForSegmentAtIndex:[sender selectedSegmentIndex]];

     if ([selAnswer isEqualToString:qACorrect])
     {
         score = score + 1;    
     }   
     else  
     {         
         score = score - 1;
     }
}

<强>更新 要获取整数值,请使用:

int qACorrect= [[sender titleForSegmentAtIndex:[sender selectedSegmentIndex]] intValue];

如果需要更多信息,请告诉我..