我正在尝试在 iOS 上继续我的问答游戏,但我遇到了问题。我成功地创建了 sqlite 数组和随机问答,但现在我无法想象如何创建 answer 方法。在我的每个问题的 sqlite 表中,answer1 列都是正确答案。那么如何创建一种方法来使用按钮检查正确答案呢?以上是随机问题和随机答案代码。
-(NSMutableArray *) categoriesList{
categories = [[NSMutableArray alloc] initWithCapacity:10];
@try {
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"Categories.sqlite"];
BOOL success = [fileMgr fileExistsAtPath:dbPath];
if(!success)
{
NSLog(@"Cannot locate database file '%@'.", dbPath);
}
if(!(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK))
{
NSLog(@"An error has occured: %s", sqlite3_errmsg(db));
}
const char *sql = "SELECT * FROM Questions WHERE Category IS 2 ORDER BY RANDOM() LIMIT 1";
sqlite3_stmt *sqlStatement;
if(sqlite3_prepare(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK)
{
NSLog(@"Problem with prepare statement: %s", sqlite3_errmsg(db));
}else{
while (sqlite3_step(sqlStatement)==SQLITE_ROW) {
Categories * choise = [[Categories alloc] init];
question.text = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 1)];
NSMutableArray *arary = [[NSMutableArray alloc]init];
while (arary.count < 4) {
int value = arc4random()%4+2;
BOOL isFound = [[arary filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:[NSString stringWithFormat:@"intValue == %d",value]]] count];
if(!isFound)
[arary addObject:[NSNumber numberWithInteger:value]];
}
answer1.text = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, [[arary objectAtIndex:0] intValue])];
answer2.text = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, [[arary objectAtIndex:1] intValue])];
answer3.text = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, [[arary objectAtIndex:2] intValue])];
answer4.text = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, [[arary objectAtIndex:3] intValue])];
[categories addObject:choise];
}
}
sqlite3_finalize(sqlStatement);
}
@catch (NSException *exception) {
NSLog(@"Problem with prepare statement: %s", sqlite3_errmsg(db));
}
@finally {
sqlite3_close(db);
return categories;
}
这是我尝试创建一个答案方法。
- (IBAction)answer1:(id)sender {
if([answer1.text isEqualToString:@"2"])
{
NSLog(@"Correct");
}else{
NSLog(@"Incorrect");
}
}
- (IBAction)answer2:(id)sender {
if([answer2.text isEqualToString:@"2"])
{
NSLog(@"Correct");
}else{
NSLog(@"Incorrect");
}
}
- (IBAction)answer3:(id)sender {
if([answer3.text isEqualToString:@"2"])
{
NSLog(@"Correct");
}else{
NSLog(@"Incorrect");
}
}
- (IBAction)answer4:(id)sender {
if([answer4.text isEqualToString:@"2"])
{
NSLog(@"Correct");
}else{
NSLog(@"Incorrect");
}
}
有人可以帮助如何使用四个按钮创建正确的答案方法吗?