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
最满意答案
不,您不必使用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
-
覆盖插件渲染器moodle(Overriding plugin renderer moodle)[2023-07-13]
您需要做两件事来让主题渲染器覆盖核心渲染器: 你需要编辑主题的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(Local Plugin permission issue not apply on course manager specifically - Moodle)[2022-09-17]
没有标准的Moodle角色称为“课程经理”。 有一个名为'经理'('经理')的角色,另一个名为'课程创建者'('coursecreator')。 如果启用了调试,如果代码有任何问题,可能会收到一些额外的警告消息(例如,如果您没有运行安装/升级过程来创建功能,或者在创建功能后没有增加插件版本号)。 I have successfully achieved the target against moodle permission. I have used has_capability method with c ... -
Moodle&vBulletin桥(Moodle & vBulletin bridge)[2022-11-06]
检查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 ...
-
Moodle插件测试(Moodle Plugin Testing)[2023-06-07]
不,您不必使用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 ...