首页 \ 问答 \ NSUserDefaults设置页面未保存(NSUserDefaults Settings Page Not Saving)

NSUserDefaults设置页面未保存(NSUserDefaults Settings Page Not Saving)

我想创建一个设置页面,它从NSUserDefaults设置/获取滑块值(文本大小),但我的应用程序没有。 我没有得到任何错误,它只是不想保存。 我已经对代码进行了评论,以帮助您更好地理解我想要做的事情,但这很简单。 我有一个具有字体大小的7级滑块,您可以简单地滑动它以确定您想要的字体大小 - 这是我想要在整个应用程序中保存的值。 但每当我按下导航栏并再次打开设置页面时,默认大小始终为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

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

原文:https://stackoverflow.com/questions/32467568
更新时间:2022-04-05 21:04

最满意答案

不,您不必使用IDE,您可以使用任何编辑器。

如果更改version.php中的版本号,则会升级该插件。

如果进行需要升级的更改(例如数据库更改或功能),则只需增加版本号。

您无需安装即可测试其他所有内容。


No you don't have to use an IDE, you can use any editor.

The plugin is upgraded if you change the version number in version.php.

You only need to increment the version number if you make changes that require an upgrade - such as database changes or capabilities.

Everything else you can test without installing.

相关问答

更多
  • 您需要先安装phpunit http://docs.moodle.org/dev/PHPUnit 然后你可以从命令行运行phpunit You will need to install phpunit first http://docs.moodle.org/dev/PHPUnit Then you can run phpunit from the command line
  • 您需要做两件事来让主题渲染器覆盖核心渲染器: 你需要编辑主题的config.php来添加这行:$ THEME-> rendererfactory ='theme_overridden_renderer_factory'; 您需要将渲染器类命名为'theme_NAMEOFTHEME_format_topcoll_renderer'(并像您一样扩展'format_topcoll_renderer')。 theme_overridden_renderer_factory通过扩展实例化渲染器以查找与名称'theme ...
  • 我找到了解决方案。 您必须在'lib.php'中的“supports”函数中添加以下行。 “case FEATURE_MOD_ARCHETYPE:return MOD_ARCHETYPE_RESOURCE;” I have found the solution. You have to add following line inside the "supports" function in 'lib.php'. "case FEATURE_MOD_ARCHETYPE: return MOD_ARCHETYP ...
  • 没有标准的Moodle角色称为“课程经理”。 有一个名为'经理'('经理')的角色,另一个名为'课程创建者'('coursecreator')。 如果启用了调试,如果代码有任何问题,可能会收到一些额外的警告消息(例如,如果您没有运行安装/升级过程来创建功能,或者在创建功能后没有增加插件版本号)。 I have successfully achieved the target against moodle permission. I have used has_capability method with c ...
  • 检查MBS的vBSSO ,适用于Moodle 2.x版本。 Check vBSSO for Moodle, working for Moodle 2.x versions.
  • 取决于你在说什么。 如果更改数据库定义,则需要upgrade.php + version.php。 如果更改语言字符串或样式表,则发布时需要更改version.php,但清除缓存将适用于开发设置(站点管理>开发>清除缓存)。 如果您更改了javascript文件,请确保您的javascript缓存设置已关闭(在Moodle网站管理员设置中)。 有时也需要清除浏览器缓存(如果这些都没有帮助,也可以执行清除缓存)。 对于新功能,事件,消息类型等。需要version.php bump(虽然这不应该经常发生)。 对 ...
  • 我做了var_dump或者输出做stdout的经验并不是trigggert。 由于快速“调试”我使用file_put_contents临时日志文件 It was a structure mistake! I put the classes folder containing observer.php in the db folder! After moving the classes folder at the root of the plugin as follow, this is ok, observ ...
  • 不,您不必使用IDE,您可以使用任何编辑器。 如果更改version.php中的版本号,则会升级该插件。 如果进行需要升级的更改(例如数据库更改或功能),则只需增加版本号。 您无需安装即可测试其他所有内容。 No you don't have to use an IDE, you can use any editor. The plugin is upgraded if you change the version number in version.php. You only need to increm ...
  • 错误消息指出您缺少文件version.php。 每个Moodle插件(不仅仅是本地插件)都需要此文件。 您可以在Moodle文档中找到本地插件所需文件的列表: https : //docs.moodle.org/dev/Local_plugins 。 此页面还链接到有关version.php文件内容的更多信息。 The error message states that you are missing the file version.php. This file is required by every ...
  • 我假设您想知道如何将CSS和JS文件包含到您的插件中。 您可以通过以下命令包含JS文件: $PAGE->requires->js( /relative/path/your_script.js'); 然后,您可以在使用以下命令下载页面后调用JS函数: $PAGE->requires->js_init_call ( your_JS_function_name, array_of_parameters_here, bool: on DOM ready); 例如: $PAGE->requires->js_ini ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。