Greywolf210
Greywolf210

Reputation: 1

how and where should I set and load NSUserDefaults in a utility app?

I have followed directions in several books and suggestions on some forums but I have issues with my app crashing when I try and set user preferences. I have the following lines on my "done" method in my flipscreenViewController:


    - (IBAction)done 
   {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setBool:musicOnOff.on forKey:kMusicPreference];
    [userDefaults setObject:trackSelection forKey:kTrackPreference];
    [self.delegate flipsideViewControllerDidFinish:self];   
   }

And the following methods in my mainViewController:


    -(void)initialDefaults
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setBool:YES forKey:kMusicPreference];
    [userDefaults setObject:@"Infinity" forKey:kTrackPreference];
}

-(void) setvaluesFromPreferences
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];   
    BOOL musicSelection = [userDefaults boolForKey:kMusicPreference];
    NSString *trackSelection = [userDefaults objectForKey:kTrackPreference];
    if(musicSelection == YES)
    {
        if([trackSelection isEqualToString:@"Infinity"])
        song = [[BGMusic alloc]initWithPath:
                       [[NSBundle mainBundle] pathForResource:
                       @"Infinity" ofType:@"m4a"]];
        else if([trackSelection isEqualToString:@"Energy"])
        song = [[BGMusic alloc]initWithPath:
                       [[NSBundle mainBundle] pathForResource:
                       @"Energy" ofType:@"m4a"]];       
        else if([trackSelection isEqualToString: @"Enforcer"])
        song = [[BGMusic alloc]initWithPath:
                       [[NSBundle mainBundle] pathForResource:
                       @"Enforcer" ofType:@"m4a"]];     
        else if([trackSelection isEqualToString: @"Continuum"])     
        song = [[BGMusic alloc]initWithPath:
                       [[NSBundle mainBundle] pathForResource:
                       @"Continuum" ofType:@"m4a"]];        
        else if([trackSelection isEqualToString: @"Pursuit"])
        song = [[BGMusic alloc]initWithPath:
                       [[NSBundle mainBundle] pathForResource:
                       @"Pursuit" ofType:@"m4a"]];

        [song setRepeat:YES];
        counter = 0;
        }
        else 
        [song close];
    }

If anyone out there could please help me see what I am doing wrong it would be much appreciated.

Chuck

Upvotes: 0

Views: 447

Answers (1)

Azeem.Butt
Azeem.Butt

Reputation: 5861

You should save your preferences whenever it makes sense to do so in the context of your application. If your code is crashing then you need to acquaint yourself with the debugger and find out why. If you want help with that, then you need to provide a stack crawl at the very least.

Upvotes: 1

Related Questions