开发者

objective-c Novice - Needs help with global variables and setter method

I am creating a quiz app which has 2 views, MMAppViewController and a subview Level1View. I have declared NSInteger property "theScore" in the MMAppViewController view and have synthesised it. In my Level1View when they answer a correct question the "theScore" int will increase by one. The score has to be a global variable because when you reach so many points it will unlock the next level.

For some reason in my switch statement it only lets me use the setTheScore method once. I am getting errors for every other set method in the switch statement. Error: "Duplicate label setTheScore". The statement is in the pushButtonAnswer method:

setTheScore: theScore++;

Here is my code:

#import "Level1View.h"
#import "MMAppViewController.h"

@implementation Level1View

@synthesize answer;
@synthesize question;
@synthesize userAnswer;
@synthesize theScore;
@synthesize score;

int questionNum=0;
NSInteger score=0;
NSInteger theScore;
BOOL start=FALSE;
BOOL optionNum=FALSE;

-(IBAction)pushBack{

    [self dismissModalViewControllerAnimated:YES];

}
-(IBAction)pushButton1{
    optionNum=TRUE;
    labelAnswer.textColor=[UIColor blackColor];
    userAnswer=@"1";
    [labelAnswer setText:(@"You chose 'A'")];
}
-(IBAction)pushButton2{
    optionNum=TRUE;
    labelAnswer.textColor=[UIColor blackColor];
    userAnswer=@"2";
    [labelAnswer setText:(@"You chose 'B'")];
}
-(IBAction)pushButton3{
    optionNum=TRUE;
    labelAnswer.textColor=[UIColor blackColor];
    userAnswer=@"3";
    [labelAnswer setText:(@"You chose 'C'")];
}
-(IBAction)pushButtonAnswer{
    labelAnswer.textColor=[UIColor blackColor];
    switch (questionNum){
        case 1:
            if(answer==userAnswer && optionNum==TRUE){
                labelAnswer.textColor=[UIColor greenColor];
                [labelAnswer setText:(@"correct")];
                [self hideButtons];
                score++;
                [self setTheScore: theScore++];
            }
            else if(optionNum==FALSE){
                [labelAnswer setText:(@"Please choose an answer below:")];}
            else{
                labelAnswer.textColor=[UIColor redColor];
                [labelAnswer setText:(@"wrong")];}
                [self hideButtons];
            break;  
        case 2:
            if(answer==userAnswer && optionNum==TRUE){
                labelAnswer.textColor=[UIColor greenColor];
                [labelAnswer setText:(@"correct")];
                [self hideButtons];
                score++;
                [self setTheScore: theScore++];
            }
            else if(optionNum==FALSE){
                [labelAnswer setText:(@"Please choose an answer below:")];}
            else{
                labelAnswer.textColor=[UIColor redColor];
                [labelAnswer setText:(@"wrong")];
                [self hideButtons];}
            break;  
        case 3:
            if(answer==userAnswer && optionNum==TRUE){
                labelAnswer.textColor=[UIColor greenColor];
                       ....

And

#import "MMAppViewController.h"
#import "Level1View.h"

@implementation MMAppViewController

@synthesize theScore;
NSInteger score;

-(IBAction)pushLevel1{

    Level1View *level1View = [[Level1View alloc] initWithNibName:nil bundle:nil];
    [self presentModalViewController:level1View animated:YES];
    theScore++;
}
-(IBAction)pushLevel2{

    //Level1View *level1View = [[Level1View alloc] initWithNibName:nil bundle:nil];
    //[self presentModalViewController:level1View animated:YES];
    NSInteger *temp = Level1View.score;
    //int theScore=2;
    [labelChoose setText:[NSString st开发者_JAVA百科ringWithFormat:@"You scored %i", theScore]];

}

Does anyone know why i am getting these errors and if I am coding this correctly?


There are a few problems here.

The primary problem is that the line setTheScore: theScore++ does not call a setter method. It's a line with a label (setTheScore:), and the only thing the line does is directly increment the variable theScore (theScore++).

The secondary problem (the reason [self setTheScore:theScore++] doesn't work) is that, contrary to your claims, the method setTheScore: doesn't appear to exist and there doesn't appear to be a theScore property of Level1View, at least in the code you've shown us.

The tertiary problem is that even if the line worked, assuming the setTheScore: method sets this theScore variable, I think you have undefined behavior (even if it's defined, it's horribly confusing and I'm not sure how it's supposed to work). theScore++ increments theScore by 1, but it returns the old, unincremented value of theScore, which is then passed to the setter method. So this line might leave theScore at the same value it was before, or it might increment the score by bypassing the setter. And it might depend on your system which one happens.

Also, I'm unclear as to whether theScore is a global variable or an instance variable. You seem to be describing both. A global variable doesn't make sense to have as a property of an instance.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜