开发者

How can I have an IBAction that has more than two 'if' statements?

开发者 https://www.devze.com 2023-02-09 16:05 出处:网络
I am creating a quiz app that will display something depending on their results of the quiz. Right now I am stuck at something. The quiz app is simple, 4 answers, and a question. Having each button sw

I am creating a quiz app that will display something depending on their results of the quiz. Right now I am stuck at something. The quiz app is simple, 4 answers, and a question. Having each button switch the view to the next question seemed to be too much code, so I 开发者_开发百科came up with this. What if each button, when pressed, would change the question and the answers? How would I do this? Right now, my code for the answer 'A' goes like this:

-(IBAction)a {

switch(questionNumber)
{
    case 0:
    {
        question.text = @"How Much Do You Use Suppressed Weapons?";
    }
        break;

    case 1:

    {
        question.text = @"Do You Like Sleight of Hand?";

        answerA.text = @"Yes";
        answerB.text = @"No";
        [answerC setHidden:YES];
        [answerD setHidden:YES];
        [answerButton3 setHidden:YES];
        [answerButton4 setHidden:YES];
    }
        break;
}

}

This isn't working, so I was hoping to go to the 'if' statement path. How exactly would I code this for each letter answer? I was thinking something like this:

-(IBAction)a { if(questionNumber = 0) { question.text = @"whatever the question is";

and then after each question it would add 1 to the question number. and the next time it would be pressed, it would change the question text to something else, and change the labels for the letter answers. Any help would be GREATLY appreciated. Thank You!!!

Related Question: Having more than one if statements in one IBAction in the .m file isn't working


A switch statement should be fine for what you are trying to do, however your syntax is a little off, use this:

-(IBAction)a {

    switch(questionNumber)
    {
        case 0:
            question.text = @"How Much Do You Use Suppressed Weapons?";
            break;
        case 1:
            question.text = @"Do You Like Sleight of Hand?";
            answerA.text = @"Yes";
            answerB.text = @"No";
            [answerC setHidden:YES];
            [answerD setHidden:YES];
            [answerButton3 setHidden:YES];
            [answerButton4 setHidden:YES];
            break;
       default:
            break;
    }
}

A good example of this can be found here: http://www.techotopia.com/index.php/The_Objective-C_switch_Statement


However, I think a better choice would be to make a question object that has all the properties you need and then have an array of those in the correct order. Then all you need do is pull the question out by index (questionNumber) and map each property to your interface. You could even go so far as to store the values in a plist and read that in so that you can easily edit/add questions on the fly without having to hardcode anything.

Here is a quick tutorial about Reading a plist into an NSArray. The second example of using NSDictionary objects might be the simplest for you. Each dictionary could be a question with the appropriate properties. Then you can just pull those values out of the dictionary (which you pulled from the Array by index).

0

精彩评论

暂无评论...
验证码 换一张
取 消