开发者

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

I am making an iPhone app that will generate something based on quiz answers. I want the button for the answer to change the question text and sometimes the button label text.

Background Info:

  • 'a' is a button
  • 'questionNumber' is the question number (duh) and it is why the button will change the texts differently each time it is pressed.

Here is the code I am using:

-(IBAction) a 
{
    questionNumber == 0;

    if(questionNumber == 0) {

        question.text = @"How Much Do You Use Suppressed Weapons?";
    }

    questionNumber == 1;

    if(questionNumber == 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];
    }
}

and this repeats for the other buttons (b,c, and d). I thought it should work, but it doesn't do the "do you like sleight of hand" question. It jus开发者_开发问答t stays at the how much do you use suppressed weapons question. Please Help! I really want to get into xcoding for the iphone.

BTW, I am not sure that the tags for this question are correct.

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


You're not assigning to questionNumber. You're comparing.

questionNumber == 1;

That's a noop. It tests if questionNumber is 1, and then throws away the result. You want

questionNumber = 1;


Try using a switch statement with breaks.

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜