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

Posted by user544006 on Stack Overflow See other posts from Stack Overflow or by user544006
Published on 2010-12-24T22:03:06Z Indexed on 2010/12/25 20:54 UTC
Read the original article Hit count: 260

Filed under:
|
|

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 stringWithFormat:@"You scored %i", theScore]];

}

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

© Stack Overflow or respective owner

Related posts about iphone

Related posts about objective-c