NSUserDefault值未正确识别

时间:2014-06-22 17:51:14

标签: ios objective-c nsuserdefaults

在这个应用程序中我试图使声音和振动的按钮在两个图像之间切换,即灰色和颜色取决于声音是否打开。单击它时,它应翻转图像并从灰色变为彩色。我已经设法让它翻转一次,从颜色到灰色,但是当我再次点击它时它不再回到颜色,请帮我解决这个问题。 提前致谢。 这是我的AppDelegate.m

if(![defaults objectForKey:@"firstTime"])
    {
        //Application is running the first time
        //Do first time stuff

        [defaults setBool:NO forKey:@"firstTime"];
        [defaults setBool:YES forKey:@"musicKey"];
        [defaults setBool:YES forKey:@"soundKey"];
        [defaults setBool:YES forKey:@"vibrationKey"];
        [defaults setBool:YES forKey:@"linkKey"];
        [defaults synchronize];

    }
    else{

        [defaults setBool:YES forKey:@"firstTime"];

    }

这是我的settingsviewcontroller,实际翻转将会发生:

-(void)viewDidLoad
{ [super viewDidLoad];
    [self checkValues];
    [self makeSettingsPage];
    self.scrollView.contentSize=[_outerView frame].size;

}


-(void)makeSettingsPage
{
    RoundButton *about=[[RoundButton alloc]initWithColor:color1 WithText:@"ABOUT"];
    RoundButton *privacyPolicy=[[RoundButton alloc]initWithColor:color2 WithText:@"PRIVACY \n POLICY"];
    RoundButton *termsOfUse=[[RoundButton alloc]initWithColor:color2 WithText:@"TERMS \n OF USE"];

    about.center=CGPointMake(29, 451);
    privacyPolicy.center=CGPointMake(29, 481);
    termsOfUse.center=CGPointMake(29, 511);
    [self.scrollView setContentMode:UIViewContentModeScaleAspectFit];


    self.scrollView.frame= self.view.frame;
//    [scrollView addSubview:viewToAdd];
//    scrollView.contentSize = viewToAdd.frame.size;
//    

    //Content Size is size of the view inside the scrollview




    self.containerView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"background_letter.png"]]];

    [self.containerView addSubview:about];
    [self.containerView addSubview:privacyPolicy];
    [self.containerView addSubview:termsOfUse];

    [self.scrollView addSubview:_containerView];
//    self.scrollView.contentSize;_containerView.frame.size;


    [self.outerView addSubview:self.scrollView];



}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}




- (IBAction)musicAction:(id)sender {

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    bool isMusicOn=[defaults boolForKey:@"musicKey"];

    if (!isMusicOn) {
        UIImage *btnImage1 = [UIImage imageNamed:@"musicgrey.png"];
        [sender setImage:btnImage1 forState:UIControlStateNormal];

    }
    else{

        UIImage *btnImage1 = [UIImage imageNamed:@"musicColor.png"];
        [sender setImage:btnImage1 forState:UIControlStateNormal];

    }

}

- (IBAction)soundAction:(id)sender
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    bool isItOn=[defaults boolForKey:@"soundKey"];

    if (!isItOn) {
        UIImage *btnImage1 = [UIImage imageNamed:@"soundcolors.png"];
        [sender setImage:btnImage1 forState:UIControlStateNormal];

    }
    else{

        UIImage *btnImage1 = [UIImage imageNamed:@"soundgrey.png"];
        [sender setImage:btnImage1 forState:UIControlStateNormal];

    }



}

- (IBAction)vibrationAction:(id)sender {

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    bool isItOn=[defaults boolForKey:@"vibrationKey"];

    if (!isItOn) {
        UIImage *btnImage1 = [UIImage imageNamed:@"virationColor.png"];
        [sender setImage:btnImage1 forState:UIControlStateNormal];

    }
    else{

        UIImage *btnImage1 = [UIImage imageNamed:@"vibrationgrey.png"];
        [sender setImage:btnImage1 forState:UIControlStateNormal];

    }



}

- (IBAction)pushAction:(id)sender {
}

- (IBAction)linkAction:(id)sender {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    bool isItOn=[defaults boolForKey:@"linkKey"];

    if (!isItOn) {
        UIImage *btnImage1 = [UIImage imageNamed:@"colorfulfacebook.png"];
        [sender setImage:btnImage1 forState:UIControlStateNormal];

    }
    else{

        UIImage *btnImage1 = [UIImage imageNamed:@"grayfacebook.png"];
        [sender setImage:btnImage1 forState:UIControlStateNormal];

    }


}

-(void)checkValues
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if([defaults boolForKey:@"musicKey"])
    {
        //Load Active Image for it
        [self.musicButton setImage:[UIImage imageNamed:@"musicColor.png"]
                          forState:UIControlStateNormal];

    }
    else{
        //Load Gray Image
        [self.musicButton setImage:[UIImage imageNamed:@"musicgrey.png"]
                          forState:UIControlStateNormal];

    }

    if([defaults boolForKey:@"soundKey"])
    {
        //Load Active Image for it
        [self.soundButton setImage:[UIImage imageNamed:@"soundcolors.png"]
                          forState:UIControlStateNormal];

    }
    else{
        //Load Gray Image
        [self.soundButton setImage:[UIImage imageNamed:@"soundgrey.png"]
                          forState:UIControlStateNormal];

    }

    if([defaults boolForKey:@"vibrationKey"])
    {
        //Load Active Image for it
        [self.vibrationButton setImage:[UIImage imageNamed:@"virationColor.png"]
                          forState:UIControlStateNormal];

    }
    else{
        //Load Gray Image
        [self.vibrationButton setImage:[UIImage imageNamed:@"vibrationgrey.png"]
                          forState:UIControlStateNormal];

    }

    if([defaults boolForKey:@"linkKey"])
    {
        //Load Active Image for it
        [self.linkButton setImage:[UIImage imageNamed:@"colorfulfacebook.png"]
                          forState:UIControlStateNormal];

    }
    else{
        //Load Gray Image
        [self.soundButton setImage:[UIImage imageNamed:@"grayfacebook.png"]
                          forState:UIControlStateNormal];

    }



}


@end

2 个答案:

答案 0 :(得分:1)

首先,这不是如何设置默认默认值:

if(![defaults objectForKey:@"firstTime"])

相反,请致电registerDefaults:。这就是它的用途。

答案 1 :(得分:0)

在没有告诉我们哪些部分没有按预期工作的情况下发布了大量代码。你应该引导我们采用一种特定的方法。

我收集你的所有IBAction方法,除了pushAction(这是空白的。)

您没有任何代码可以更改用户默认值中的切换状态,因此每次执行代码时, if 语句都会执行相同的操作。

你需要添加一行来翻转bool。像这样:

NSString *key = @"musicKey" 
bool isMusicOn=[defaults boolForKey: key];
[defaults setBool: !isMusicOn] forKey: key];

另请注意Matt关于registerDefaults的建议。这是设置起始默认值的更好方法(在Xcode文档中搜索" registerDefauts"

最后,我建议将所有按钮代码折叠为1个动作方法。为所有按钮添加标记值。在viewDidLoad中,设置一个键字符串数组。在您的IBAction方法中,使用sender.tag获取按钮的标记值,并使用它来索引键字符串数组。

(作为一般规则,当您发现自己一遍又一遍地重复相同的代码时只会改变一些事情,这表明您应该重新考虑您的方法。将相同的代码复制粘贴到过度繁琐,难以维护,容易出错。)