Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/100.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 引用另一方法objective-c中的变量_Ios_Objective C_Xcode - Fatal编程技术网

Ios 引用另一方法objective-c中的变量

Ios 引用另一方法objective-c中的变量,ios,objective-c,xcode,Ios,Objective C,Xcode,我是Obj-C的新手,如果问题很明显,我深表歉意 无论如何,我只是进入objective-c并尝试创建一个基本的iOS计数器,用户每次单击/触摸UI时,一个点计数器都会递增 我已经创建了currentScore int并可以将其记录到控制台。我还成功地将UI的每次触摸记录到控制台 我现在要做的是,在每次触摸时访问currentScore int,并将int增加1 正如我所说,可能非常简单,但不是100%确定如何在其他函数中引用其他变量 提前谢谢 // // JPMyScene.m // #i

我是Obj-C的新手,如果问题很明显,我深表歉意

无论如何,我只是进入objective-c并尝试创建一个基本的iOS计数器,用户每次单击/触摸UI时,一个点计数器都会递增

我已经创建了currentScore int并可以将其记录到控制台。我还成功地将UI的每次触摸记录到控制台

我现在要做的是,在每次触摸时访问currentScore int,并将int增加1

正如我所说,可能非常简单,但不是100%确定如何在其他函数中引用其他变量

提前谢谢

//
//  JPMyScene.m
//

#import "JPMyScene.h"

@implementation JPMyScene

-(id)initWithSize:(CGSize)size {    
    if (self = [super initWithSize:size]) {
        /* Setup your scene here */

        self.backgroundColor = [UIColor redColor];

        //Initiate a integer point counter, at 0.

        int currentScore = 0;
        NSLog(@"Your score is: %d", currentScore);

    }
    return self;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    /* Called when a touch begins */

    for (UITouch *touch in touches) {

        NSLog(@"Tap, tap, tap");

        //TODO: For each tap, increment currentScore int by 1

    }
}

-(void)update:(CFTimeInterval)currentTime {
    /* Called before each frame is rendered */

}

@end

您的问题与ObjC无关,但通常与OOP有关。基本上,您需要一个在对象的多个方法中可用的变量。解决方案是将currentScore声明为JPMyScene的实例变量。

首先在interface.h接口中将整数声明为全局变量或属性。然后从您想要的任何位置访问和修改。以下是您的接口和实现:

@interface JPMyScene
{
int currentScore;
}
@end

@implementation JPMyScene

-(id)initWithSize:(CGSize)size {    
    if (self = [super initWithSize:size]) {

        self.backgroundColor = [UIColor redColor];
        currentScore = 0;
        NSLog(@"Your score is: %d", currentScore);
    }
  return self;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

     for (UITouch *touch in touches) {
        NSLog(@"Tap, tap, tap");
        currentScore++;
     }
}

-(void)update:(CFTimeInterval)currentTime {

}

将currentScore变量更改为全局变量,如此代码中所示

//
//  JPMyScene.m
//

#import "JPMyScene.h"

@implementation JPMyScene
int currentScore = 0;
-(id)initWithSize:(CGSize)size {
            /* Setup your scene here */

        self.view.backgroundColor = [UIColor redColor];

        //Initiate a integer point counter, at 0.


        NSLog(@"Your score is: %d", currentScore);

    return self;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    /* Called when a touch begins */

    for (UITouch *touch in touches) {

        NSLog(@"Tap, tap, tap");
        currentScore++;
       NSLog(@"Your score is: %d", currentScore);
        //TODO: For each tap, increment currentScore int by 1

    }
}

-(void)update:(CFTimeInterval)currentTime {
    /* Called before each frame is rendered */

}

@end

全局变量?请不要在OOP中教初学者这样的东西,事情很容易从他们的手中消失。顺便说一句,一个静态的会有同样的效果,但没有讨厌的“全球性”。