2015-09-08 64 views
0

我想創建一個設置頁面,其中它設置/從NSUserDefaults獲取滑塊值(文本大小),但我的應用程序沒有它。我沒有得到任何錯誤,它只是不想保存。我已經評論了代碼,以幫助您更好地理解我想要做什麼,但它非常簡單。我有一個具有字體大小的7級滑塊,您可以將其滑動以確定您想要的字體大小 - 這是我希望在整個應用程序中保存的值。但每當我回壓在導航欄上,再次打開設置頁面,默認的大小始終是22NSUserDefaults設置頁面不保存

.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 
+1

您的保存該值的代碼可保存點的大小,但加載該值的代碼嘗試將滑塊的索引設置爲保存的字體大小。這不一致,需要修復。 – rmaddy

+0

我編輯了代碼來顯示字體大小的變化。 – Jose

+1

你錯過了我的觀點。在'viewDidLoad'中,你可以調用'self.sizeSlider.value = textsize;'。但滑塊的值不是文本大小,它是大小數組的索引。 – rmaddy

回答

0

答案來自rmaddy - 謝謝!我需要看看索引,而不是實際的數組值。

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

所以我把它放在那裏,並且返回了我想要的滑塊位置的索引。