我已经为此苦苦挣扎了好几天了;我的应用程序有一个带有 uitextfields 的图表来表示图片的标签。我想根据字典检查用户输入(以获得答案),如果它是正确的,则将分数增加 1。我通过“硬编码”每个 textfield.text 查询来工作,每个查询都有自己的 if 语句,但如果可能的话,我想要一种更好、更可重用的方法?到目前为止,我已经尝试过:
- (IBAction)checkAnswers:(UITextField *)textField
{
// array for each textfield
allTextfields = [[NSArray alloc] initWithObjects:eyepiece, objectiveLenses, focussingKnobs, stage, mirror, nil];
// array for each UIImageView
allTicks = [[NSArray alloc] initWithObjects:eyepieceTick, objectiveTick, focussingTick, stageTick, mirrorTick, nil];
UIImage *image = [UIImage imageNamed:@"Tick.png"];
for (textField in allTextfields) {
if ([textField.text isEqualToString:[[microscopeBrain.microscopeDictionary valueForKey:theTextfieldTag] valueForKey:@"Answer"]]) {
[[allTicks objectAtIndex:textField.tag] setImage:image];
x++;
textField.enabled = NO;
NSLog(@"%@", microscopeBrain.microscopeDictionary);
// NSLog(@"%@", [[microscopeBrain.microscopeDictionary valueForKey:theTextfieldTag] valueForKey:@"Answer"]);
}
finalMicroscopeScore = [[NSString alloc] initWithFormat:@"%i", x];
microscopeScoreLabel.text = [[NSString alloc] initWithFormat:@"%i", x];
}
}
问题是即使答案在错误的文本字段中,只要一个是正确的,它们都会显示为正确的,这有点尴尬!任何帮助将不胜感激。