Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/41.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.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
Iphone cocos2d中的NSUserDefaults不工作_Iphone_Objective C_Cocos2d Iphone - Fatal编程技术网

Iphone cocos2d中的NSUserDefaults不工作

Iphone cocos2d中的NSUserDefaults不工作,iphone,objective-c,cocos2d-iphone,Iphone,Objective C,Cocos2d Iphone,我在这里有一个Button,当按下时将1添加到myValue并将其存储到默认值 #import "HelloWorldLayer.h" #import "Player.h" #import "MyObject.h" //#import "SettingsManager.h" // HelloWorldLayer implementation @implementation HelloWorldLayer @synthesize myObject,newPlayer; +(CCScene *)

我在这里有一个Button,当按下时将1添加到myValue并将其存储到默认值

#import "HelloWorldLayer.h"
#import "Player.h"
#import "MyObject.h"
//#import "SettingsManager.h"

// HelloWorldLayer implementation
@implementation HelloWorldLayer
@synthesize myObject,newPlayer;

+(CCScene *) scene
{
    // 'scene' is an autorelease object.
    CCScene *scene = [CCScene node];

    // 'layer' is an autorelease object.
    HelloWorldLayer *layer = [HelloWorldLayer node];

    // add layer as a child to scene
    [scene addChild: layer];

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
-(id) init
{
    // always call "super" init
    // Apple recommends to re-assign "self" with the "super" return value
    if( (self=[super init])) {
        usrDef = [NSUserDefaults standardUserDefaults];
        //CGSize
        CGSize winSize = [[CCDirector sharedDirector]winSize];
        //settingsmangaer
        //myValue = [[SettingsManager sharedSettingsManager] getInt:@"hiscore"];

        //child
        newPlayer = [[Player alloc]init];
        [self addChild:newPlayer];
        CCMenuItem *myMenuItem = [CCMenuItemImage itemFromNormalImage:@"Button0000.png" selectedImage:@"Button0001.png" target:self selector:@selector(buttonPressed:) ];
        CCMenu *myMenu = [CCMenu menuWithItems:myMenuItem, nil];
        myMenu.position = ccp(50, 50);
        [self addChild:myMenu];
        //cclabel
        myValue = [usrDef integerForKey:@"difficulty"];
        myLabel = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"Hello: %d",myValue]  fontName:@"Arial" fontSize:50];
        [myLabel setColor:ccBLACK];
       // [myLabel setCString:@"hihi: %d",myValue];
        myLabel.position = ccp(winSize.width/2,winSize.height/2);
        [self addChild:myLabel];

    }

    return self;
}
我使用的nsdefault不起作用。重新打开应用程序时,标签再次从0开始。程序编译正确,我没有收到任何错误。请帮忙,谢谢

-(void)buttonPressed:(id)sender{
    myValue +=1;
    [myLabel setString:[NSString stringWithFormat:@"Hihi: %d",myValue]];
    [usrDef setInteger:myValue forKey:@"diffculty"];
    [usrDef synchronize];
}

// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
    // in case you have something to dealloc, do it in this method
    // in this particular example nothing needs to be released.
    // cocos2d will automatically release all the children (Label)

    // don't forget to call "super dealloc"
    [super dealloc];
}

@end
以上是您想要执行的操作的正确语法

下面是如何检索数据

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

-(void)buttonPressed:(id)sender{
    myValue +=1;
    [myLabel setString:[NSString stringWithFormat:@"Hihi: %d",myValue]];
    [defaults setInteger:myValue forKey:@"diffculty"];
    [defaults synchronize];
}

您的代码拼错了难度^ ^谢谢。。。。这就是我恨自己的原因。没什么大不了的,我只是在和你开玩笑:)
- (void)viewDidLoad
{
    // Get the stored data before the view loads
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    NSString *firstName = [defaults objectForKey:@"firstName"];
    NSString *lastName = [defaults objectForKey:@"lastname"];

    int age = [defaults integerForKey:@"age"];
    NSString *ageString = [NSString stringWithFormat:@"%i",age];

    NSData *imageData = [defaults dataForKey:@"image"];
    UIImage *contactImage = [UIImage imageWithData:imageData];

    // Update the UI elements with the saved data
    firstNameTextField.text = firstName;
    lastNameTextField.text = lastName;
    ageTextField.text = ageString;
    contactImageView.image = contactImage;

    [super viewDidLoad];
}