吸气剂setter int value(NSObject)没有从一个ViewController传递给另一个

时间:2014-06-01 14:24:14

标签: ios objective-c uiviewcontroller

我正在构建一个有3个不同级别难度的游戏(简单,中等和删除)我有2个ViewControllers和1个​​NSObject类,我用来设置和获取游戏难度值(0 =简单,1 =中等,2 =删除(硬)。在我如何做的背景下,我进行了研究并尝试了许多变化而没有成功。注意:我试图对此进行编码,但是因为它没有工作,我已经删除了那部分代码。我对NSUserDefaults有点熟悉,所以如果这是一个更好的解决方案,请指教。我可能需要一点指示设置但是,起来。
- ViewController 1(RewindSettingsViewController):我有3个按钮:Easy,Medium,Delete。用户点击按钮,他们转到ViewController 2
- ViewController 2(RewindGameViewController):这是带有“开始”按钮的实际游戏。这就是我挂断的地方;让困难传递到这个观点。

CODE:
.h - GameManager:NSObject

+(void) setDifficulty:(int)difficulty;
+(int) getDifficulty;

.m - GameManager

static int _difficulty;

+(void)setDifficulty:(int)difficulty {
    _difficulty = difficulty;
}

+(int)getDifficulty {
    return _difficulty;
}

(VC1).h - RewindSettingsViewController:UIViewController

@property (nonatomic, strong) IBOutlet UIButton *easyModeButton;
@property (nonatomic, strong) IBOutlet UIButton *mediumModeButton;
@property (nonatomic, strong) IBOutlet UIButton *hardModeButton;

-(IBAction)easyMode;
-(IBAction)mediumMode;
-(IBAction)hardMode;

.m - RewindSettingsViewController

-(IBAction)easyMode {
    [GameManager setDifficulty:0];
}

-(IBAction)mediumMode {
    [GameManager setDifficulty:1];
}

-(IBAction)hardMode {
    [GameManager setDifficulty:2];
}

(VC2).h - RewindGameViewController:UIViewController

此处没有设置难度设置的代码

.m - RewindGameViewController

此时,_difficulty当然仍然是零。我的问题是我的痛点

?:如何将难度值存储在" setDifficulty"进入" getDifficulty(_difficulty)"并在IF声明中正确地称之为?
?:我认为_difficulty应该在IF语句中,但是在我尝试的所有内容中,语法一直在咆哮。

-(void)placeObjectsRewind {

    randomTopObjectRewind = arc4random() %350;  
    randomTopObjectRewind = randomTopObjectRewind - 228;

    if (?) {

    randomBottomObjectRewind = randomTopObjectRewind + 700; //LEVEL DIFFICULTY: EASY
        objectTopRewind.center = CGPointMake(0, randomTopObjectRewind);
        objectBottomRewind.center = CGPointMake(0, randomBottomObjectRewind);

    } else if (?) {

    randomBottomObjectRewind = randomTopObjectRewind + 665; //LEVEL DIFFICULTY: MEDIUM
        objectTopRewind.center = CGPointMake(0, randomTopObjectRewind);
        objectBottomRewind.center = CGPointMake(0, randomBottomObjectRewind);

    } else if (?) {

        randomBottomObjectRewind = randomTopObjectRewind + 635; //LEVEL DIFFICULTY: DELETE (HARD)
            objectTopRewind.center = CGPointMake(0, randomTopObjectRewind);
            objectBottomRewind.center = CGPointMake(0, randomBottomObjectRewind);
    }
}

2 个答案:

答案 0 :(得分:2)

而不是

static int _difficulty;

在GameManager.h中输入

@property int difficulty;

您不必自己编写Setter和Getter,属性声明会为您执行此操作。 Getter / Setter存储在您使用的GameManager实例中。因此,获取GameManager的实例,然后设置如下:

[MyGameManagerInstance setDifficulty:0]; 

并得到像

[MyGameManagerInstance difficulty];

由于难度属性位于.h文件中,因此它是Class API的一部分,您可以从其他ViewController或类访问它。

编辑: 就像你说的那样,你也可以用NSUserDefaults做到这一点。就这样说吧:

[[NSUserDefaults standardUserDefaults] setInteger:1 forKey:@"Difficulty"];

并且这样:

int myInt = [[NSUserDefaults standardUserDefaults] integerForKey:@"Difficulty"];

答案 1 :(得分:2)

由于您已经创建了要使用类方法的方法,因此语法将类似于if ([GameManager getDifficulty] == 0)等。

(作为一个样式点,您通常会删除Objective-C中的“get”,并将该方法命名为返回值difficulty。)