Search code examples
iosnsuserdefaultsuislider

NSUserDefaults Settings Page Not Saving


I would like to create a settings page, where it sets/gets the slider value (text Size) from NSUserDefaults, but my app is having none of it. I dont get any errors, it just doesn't want to save. I've commented the code, to help you understand better what I want to do, but it's quite simple. I have a 7-stage slider that has the font sizes, and you can simply slide it to determine what size of font you want - that is the value I want saved throughout the app. But whenever I press back on the navigation bar and open the settings page again, the default size is always 22.

.h

@interface SettingsViewController : UIViewController

@property (strong, nonatomic) IBOutlet UISlider *sizeSlider;
@property (strong, nonatomic) IBOutlet UILabel *sampleText;

@end

.m

@interface SettingsViewController () {
    NSArray *numbers;
    NSMutableAttributedString *sampleTextString;
    int fontSize;
    NSUserDefaults *defaults;
}

@end

@implementation SettingsViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationItem.title = @"Settings";

    numbers = @[@(10), @(12), @(14), @(16), @(18), @(20), @(22)];                 // Text Sizes, these number values represent each slider position
    NSInteger numberOfSteps = ((float)[numbers count]-1);                           // Slider values go from 0 to the number of values in your numbers array

    self.sizeSlider.maximumValue = numberOfSteps;                                   // As the slider moves it will continously call the -valueChanged:
    self.sizeSlider.minimumValue = 0;


    defaults = [NSUserDefaults standardUserDefaults];                                //Setting the font to be the current font saved in the system
    long textsize = [defaults integerForKey:@"fontSize"];
    NSLog(@"textsize: %ld", textsize);
    self.sizeSlider.value = textsize;                                               // Set the Slider to whatever font you had set it previously



    self.sizeSlider.continuous = YES;                                               // NO makes it call only once you let go
    [self.sizeSlider addTarget:self action:@selector(valueChanged:) forControlEvents:UIControlEventValueChanged];

    sampleTextString = [[NSMutableAttributedString alloc] initWithString:@"The Quick Brown Fox Jumps Over The Lazy Dog!"];
    self.sampleText.attributedText = sampleTextString;

    [sampleTextString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:textsize] range:NSMakeRange(0, sampleTextString.length)];

}


-(void)valueChanged:(UISlider *)sender{
    NSUInteger index = (NSUInteger)(self.sizeSlider.value + 0.5);       // round the slider position to the nearest index of the numbers array
    [self.sizeSlider setValue:index animated:NO];


    NSNumber *number = numbers[index];                                  // <-- This numeric value you want
    [sampleTextString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:[number floatValue]] range:NSMakeRange(0, sampleTextString.length)];

    [defaults setInteger:[number integerValue] forKey:@"fontSize"];         // Saving fontSize to NSUserDefaults
    [defaults synchronize];

    self.sampleText.attributedText = sampleTextString;
}
@end

Solution

  • Answer comes from rmaddy - Thanks!

    I needed to look at the index, rather than the actual value of the array.

    NSInteger indexText = [numbers indexOfObject:[NSNumber numberWithInteger:textsize]];
    

    So I put that in, and that returned the index of the slider position I wanted.