我如何拥有一个包含两个以上"if"语句的IBAction? [英] How can I have an IBAction that has more than two 'if' statements?

查看:42
本文介绍了我如何拥有一个包含两个以上"if"语句的IBAction?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个测验应用程序,该应用程序将根据测验结果显示某些内容.现在,我陷入了困境.测验应用程序很简单,有4个答案和一个问题.让每个按钮将视图切换到下一个问题似乎是太多的代码,所以我想到了这一点.如果每个按钮在按下时都会改变问题和答案该怎么办?我该怎么做?现在,我对答案"A"的代码如下:

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;
}

}

这不起作用,所以我希望转到'if'语句路径.对于每个字母答案,我将如何精确编码?我在想这样的事情:

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 = @无论问题在哪里";

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

,然后在每个问题之后将问题编号加1.下次将其按下时,会将问题文本更改为其他内容,并更改字母答案的标签.任何帮助将不胜感激.谢谢!!!

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!!!

相关问题:推荐答案

对于您要尝试执行的操作来说,switch语句应该可以,但是您的语法有些偏离,请使用以下方法:

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;
    }
}

一个很好的例子可以在这里找到: http://www.techotopia.com/index.php/The_Objective-C_switch_Statement

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

但是,我认为更好的选择是使一个问题对象具有所需的所有属性,然后按正确的顺序排列这些属性.然后,您所需要做的就是通过索引(questionNumber)提取问题并将每个属性映射到您的界面.您甚至可以将值存储在plist中,然后读入其中,这样就可以轻松地即时编辑/添加问题,而无需进行任何硬编码.

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.

这里是有关阅读plist进入NSArray .使用NSDictionary对象的第二个示例对您来说可能是最简单的.每个字典可能是一个具有适当属性的问题.然后,您只需将这些值从字典中拉出即可(通过索引将其从Array中拉出).

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).

这篇关于我如何拥有一个包含两个以上"if"语句的IBAction?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆