简体   繁体   English

Getter和Setter int值(NSObject)没有从一个ViewController传递到另一个

[英]Getter & setter int value (NSObject) not passing from one ViewController to another

I'm building a game with 3 different level difficulties (Easy, Medium, & Delete) I have 2 ViewControllers and 1 NSObject class that I'm using to set and get the game difficulty value (0=easy, 1=Medium, 2=Delete(hard). Within context of how I'm doing it, I've research and tried many variations with no success. NOTE: I have tried to code this, but have since stripped out that part of the code because it wasn't working. I'm somewhat familiar with NSUserDefaults, so if that's a better solution, please advise. I may need a little direction on setting that up, though. 我正在构建具有3个不同关卡难度(轻松,中等和删除)的游戏,我使用2个ViewController和1个NSObject类来设置和获取游戏难度值(0 =简单,1 =中等,2 = Delete(hard)。在我做这件事的背景下,我已经研究并尝试了许多变体,但均未成功注意:我试图对此进行编码,但此后由于没有没用。我对NSUserDefaults有点熟悉,因此,如果这是一个更好的解决方案,请告知。尽管如此,我可能需要一些指导。
- ViewController 1 (RewindSettingsViewController): I have 3 buttons: Easy, Medium, Delete. -ViewController 1(RewindSettingsViewController):我有3个按钮:“简单”,“中”,“删除”。 User taps button, they go to ViewController 2 用户点击按钮,他们转到ViewController 2
- ViewController 2 (RewindGameViewController): This is the actual game with a Start button. -ViewController 2(RewindGameViewController):这是带有“开始”按钮的实际游戏。 This is where I'm hung up; 这是我挂断电话的地方; getting the difficulty passed into this view. 将困难转移到此视图中。
CODE: 码:
.h - GameManager : NSObject .h-GameManager:NSObject

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

.m - GameManager .m-GameManager

static int _difficulty;

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

+(int)getDifficulty {
    return _difficulty;
}

(VC1) .h - RewindSettingsViewController : UIViewController (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 .m-RewindSettingsViewController

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

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

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

(VC2) .h - RewindGameViewController : UIViewController (VC2).h-RewindGameViewController:UIViewController

no code here for difficulty settings 此处没有用于难度设置的代码

.m - RewindGameViewController .m-RewindGameViewController

At this point, _difficulty is of course still nil. 在这一点上,_difficulty当然仍然为零。 My question marks below are my pain points 我下面的问号是我的痛点

?: How to get the difficulty value stored in "setDifficulty" into "getDifficulty (_difficulty)" and properly call it in the IF statement? ?:如何将存储在“ setDifficulty”中的难度值转换为“ getDifficulty(_difficulty)”并在IF语句中正确调用?
?: I'm thinking _difficulty should be in the IF statements, but with everything I'm trying, syntax keeps barking at me. ?:我在想_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);
    }
}

Instead of 代替

static int _difficulty;

type this into your GameManager.h 将此输入您的GameManager.h

@property int difficulty;

You don't have to write the Setter and Getter by yourself, the property-statement do this for you. 您不必自己编写Setter和Getter,属性声明可以为您完成此操作。 The Getter/Setter is stored in the instance of GameManager you use. Getter / Setter将存储在您使用的GameManager实例中。 So get the instance of you GameManager and there you set like this: 因此,获取您的GameManager实例,并在其中进行如下设置:

[MyGameManagerInstance setDifficulty:0]; 

and get like 并得到像

[MyGameManagerInstance difficulty];

Due to the fact that the difficulty-property is in the .h file, it's part of the Class's API which you can access from other ViewController or Classes. 由于困难属性位于.h文件中,因此它是类API的一部分,您可以从其他ViewController或类访问它。

EDIT: Like you said, you could do this also with the NSUserDefaults. 编辑:就像你说的,你也可以使用NSUserDefaults来做到这一点。 Just put it in like this: 像这样把它放进去:

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

and get it like this: 并得到这样的:

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

Since you've made the methods you want to use class methods, the syntax is going to be similar to if ([GameManager getDifficulty] == 0) etc. 由于已经创建了要使用类方法的方法,因此语法将类似于if ([GameManager getDifficulty] == 0)等。

(As a style point, you would usually drop the "get" in Objective-C and just name the method to return a value difficulty .) (作为样式点,通常可以在Objective-C中删除“ get”,而仅命名方法以返回值difficulty

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

相关问题 将 NSString 从 ViewController 传递给 NSObject - Passing NSString from a ViewController to a NSObject 使用segue将Int从一个ViewController传递到另一个 - passing an Int from one ViewController to another using a segue Swift:将值从一个类传输到另一个类或从 ViewController 类到 NSObject 的特定值 - Swift: Transfer value from one class to another or to be specific from ViewController class to NSObject 从NSObject传递NSMutableArray到ViewController时出错 - error in passing NSMutableArray from NSObject To ViewController 使参数从一个ViewController传递到另一个的崩溃 - crash passing parameter from one viewcontroller to another 将UISlider值从一个ViewController传递到另一个 - Passing UISlider values from one ViewController to another 将对象从一个 ViewController 传递到另一个 - Passing the Object from one ViewController to another 将数据从一个ViewController传递到另一个ViewController - Passing data from one viewcontroller to another 将字符串从一个ViewController传递到另一个TabbarController - Passing a string from one viewcontroller to another Tabbarcontroller 将一个ViewController中的managedObjectContext传递给Swift中的另一个ViewController? - Passing an managedObjectContext from one ViewController to another ViewController in Swift?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM