简体   繁体   English

objective-c新手 - 需要帮助全局变量和setter方法

[英]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. 我正在创建一个测验应用程序,它有2个视图,MMAppViewController和子视图Level1View。 I have declared NSInteger property "theScore" in the MMAppViewController view and have synthesised it. 我在MMAppViewController视图中声明了NSInteger属性“theScore”并合成了它。 In my Level1View when they answer a correct question the "theScore" int will increase by one. 在我的Level1View中,当他们回答正确的问题时,“ theScore” int将增加一。 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. 由于我的switch语句中的某些原因,它只允许我使用一次setTheScore方法。 I am getting errors for every other set method in the switch statement. 我在switch语句中遇到每个其他set方法的错误。 Error: "Duplicate label setTheScore". 错误:“重复标签setTheScore”。 The statement is in the pushButtonAnswer method: 该语句在pushButtonAnswer方法中:

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? 有谁知道我为什么会收到这些错误以及我是否正确编写了这些代码?

There are a few problems here. 这里有一些问题。

The primary problem is that the line setTheScore: theScore++ does not call a setter method. 主要问题是setTheScore: theScore++行未调用setter方法。 It's a line with a label ( setTheScore: ), and the only thing the line does is directly increment the variable theScore ( theScore++ ). 这是带有标签的行( setTheScore: ,该行唯一要做的就是直接增加变量theScoretheScore++ )。

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. 第二个问题( [self setTheScore:theScore++]不起作用的原因)是,与您的主张相反, setTheScore:方法似乎不存在,并且似乎不存在theScore属性。至少在您向我们展示的代码中。

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). 第三个问题是,即使该行工作了,假设使用setTheScore:方法设置了theScore变量,我也认为您有未定义的行为(即使已定义,也很令人困惑,我不确定它应该如何工作)。 theScore++ increments theScore by 1, but it returns the old, unincremented value of theScore , which is then passed to the setter method. theScore++增量theScore 1,但它返回旧,unincremented值theScore ,然后将其传递给设置器方法。 So this line might leave theScore at the same value it was before, or it might increment the score by bypassing the setter. 因此,此行可能使theScore保持与之前相同的值,或者可能通过绕过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. 此外,我不清楚theScore是全局变量还是实例变量。 You seem to be describing both. 您似乎在描述两者。 A global variable doesn't make sense to have as a property of an instance. 作为实例的属性,全局变量没有意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM