创建将从文本文件中提取的下一个按钮

时间:2014-02-16 15:18:24

标签: ios iphone objective-c

我创建了一个使用文本文件来提问的测验。我不知道如何解决所有问题。我认为,如果我要创建一个int,每当有人选择一个问题直到它们通过它们时会增加,所有这些都会起作用。我将如何创建随每个问题而增加的int并实现下一个按钮。测验的设置方式是在一个视图控制器上,它有一个标签,它更改为文本文件中的第一行文本,四个按钮将更改为接下来的四行。一旦选择了一个按钮并且正确的过程注意它是否正确运行,这反过来隐藏了四个按钮并显示一个带有最后一行的标签(在文本文件中以*结尾表示它是正确答案)。现在我只能让它在每个文本文件中运行第一个问题。 int方法是我认为可行的唯一方法,但我对其他想法持开放态度。非常感谢你!我已经慢慢找到了一种让测验工作的方法,一旦我这样做,我会把它发布到github上,以便那些一直关注这个的人会看到我如何拥有一切。

- (void)loadFirstCategory {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"FirstVolume"
                                                     ofType:@"txt"];
    NSString *content = [NSString stringWithContentsOfFile:path
                                                  encoding:NSUTF8StringEncoding
                                                     error:NULL];
    NSArray *lines = [content componentsSeparatedByString:@"\n"];
    // Line is going to have @[Question, Answer, ..., * Correct Answer]

    NSMutableArray *questionAnswers = [NSMutableArray array];

    BOOL loadedQuestion = NO;

    QuestionAnswer *questionAnswer = [QuestionAnswer new];

    for(NSString *line in lines) {
        if(!loadedQuestion) {
            loadedQuestion = YES;
            questionAnswer.question = line;
            continue;
        }

        if([line isEqualToString:@""]) {
            [questionAnswers addObject:questionAnswer];
            questionAnswer = [QuestionAnswer new];
            loadedQuestion = NO;
            continue;
        }

        NSString *firstCharacter = [line substringToIndex:1];

        if([firstCharacter isEqualToString:@"*"]) {
            NSString *correctAnswer = [line substringWithRange:NSMakeRange(2, line.length - 2)];
            questionAnswer.rightAnswer = correctAnswer;
            continue;
        } else {
            [questionAnswer.possibleAnswers addObject:line];
            continue;
        }
    }

    QuestionAnswer *tempQuestionAnswer = [questionAnswers objectAtIndex:0];

    QuestionText.text = tempQuestionAnswer.question;

    [Answer1 setTitle:@"" forState:UIControlStateNormal];
    [Answer2 setTitle:@"" forState:UIControlStateNormal];
    [Answer3 setTitle:@"" forState:UIControlStateNormal];
    [Answer4 setTitle:@"" forState:UIControlStateNormal];

    if(tempQuestionAnswer.possibleAnswers.count >= 1) {
        [Answer1 setTitle:[tempQuestionAnswer.possibleAnswers objectAtIndex:0] forState:UIControlStateNormal];
    }

    if(tempQuestionAnswer.possibleAnswers.count >= 2) {
        [Answer2 setTitle:[tempQuestionAnswer.possibleAnswers objectAtIndex:1] forState:UIControlStateNormal];
    }

    if(tempQuestionAnswer.possibleAnswers.count >= 3) {
        [Answer3 setTitle:[tempQuestionAnswer.possibleAnswers objectAtIndex:2] forState:UIControlStateNormal];
    }

    if(tempQuestionAnswer.possibleAnswers.count >= 4) {
        [Answer4 setTitle:[tempQuestionAnswer.possibleAnswers objectAtIndex:3] forState:UIControlStateNormal];
    }

    CorrectAnswerDisplay.text = tempQuestionAnswer.rightAnswer;

    NSInteger indexOfCorrectAnswer = 0;

    for(NSString *possibleAnswer in tempQuestionAnswer.possibleAnswers) {
        if([possibleAnswer isEqualToString:tempQuestionAnswer.rightAnswer]) {
            break;
        }

        indexOfCorrectAnswer++;
    }

    if(indexOfCorrectAnswer == 0) Answer1Correct = YES;
    else if(indexOfCorrectAnswer == 1) Answer2Correct = YES;
    else if(indexOfCorrectAnswer == 2) Answer3Correct = YES;
    else if(indexOfCorrectAnswer == 3) Answer4Correct = YES;
}

以下是我的文本文件。

Question on the first line
Answer
Answer
Answer
Answer
* Correct Answer that is displayed separately

Next question goes here
Answer
Answer
Answer
Answer
* Correct Answer that is displayed separately

每个文本文件中都有大约200多个问题。

2 个答案:

答案 0 :(得分:0)

我不知道你是否有使用文本文件的限制,但在iOS上我建议你使用plist,操作系统知道如何管理它,并且它很容易编辑。

答案 1 :(得分:0)

以下是一些可能对您有帮助的代码。首先创建一个类来保存问题。这是代码。     //Question.h     #import

@interface Question : NSObject
@property(nonatomic,strong)NSMutableArray *answers;
@property(nonatomic,strong)NSString *question,*answer;
-(id)init;
-(void)addAnswer:(NSString *)param;
@end

//Question.m
#import "Question.h"

@implementation Question
@synthesize question,answer,answers;
-(id)init{
    self = [super init];
    if (self) {
        NSLog(@"Question created");
        answers = [[NSMutableArray alloc] init];
        return self;
    }
    else return nil;
}
-(void)addAnswer:(NSString *)param{
    [answers addObject:param];
}
@end

现在在视图控制器中添加这些行

    NSUInteger numberOfOccurrences = [[content componentsSeparatedByString:@"*"] count] - 1;

    NSMutableArray *questions = [[NSMutableArray alloc] init];

    for (int i=0; i<numberOfOccurrences; i++) {
        [questions addObject:[[Question alloc] init]];
    }
    BOOL addingNewQuestion=YES;
    BOOL addingAnswer=NO;

    int questionIndex=0;

    for(NSString *line in lines) {
        NSLog(@"%@",line);
        if (addingNewQuestion) {
            [[questions objectAtIndex:questionIndex] setQuestion:line];
            addingNewQuestion=NO;
            addingAnswer=YES;
        }
        else if(addingAnswer){
            if ([line isEqualToString:@""]) {
                NSLog(@"Adding new question");
                addingNewQuestion=YES;
                addingAnswer=NO;
                questionIndex++;
            }
            else if([[line substringToIndex:1] isEqualToString:@"*"]){
                NSLog(@"Adding correct answer");
                [[questions objectAtIndex:questionIndex] setAnswer:[line substringWithRange:NSMakeRange(2, line.length - 2)]];
            }
            else {
                NSLog(@"Adding answer");
                [[questions objectAtIndex:questionIndex] addAnswer:line];
            }
        }
    }

现在你有一个名为问题的数组,每个成员都是一个问题对象,有自己的问题,答案数组和正确答案。您可以使用for循环访问每个问题。

for(int i = 0;i<[questions count];i++){
    [self handleQuestion:[questions objectAtIndex:i]];
}

我没有写handleQuestion方法。做你喜欢的任何事情。