iPhone滑块保存用户偏好
我需要在 Settings.bundle 中保存滑块首选项。
我使用以下代码写入 Settings.bundle:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:mySlider.value forKey:@"TimerSliderKey"];
[defaults synchronize];
并使用以下代码从 Settings.bundle 读取:
NSUserDefaults *defaults2 = [NSUserDefaults standardUserDefaults];
int valueForSlider = [defaults2 integerForKey:@"TimerSliderKey"];
NSLog(@"The saved slider value is %@" , valueForSlider);
此代码不起作用。有人可以给我一些我可以使用的替换代码吗?
I need to save a slider preference in my Settings.bundle.
I'm using the following code to write to the Settings.bundle:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:mySlider.value forKey:@"TimerSliderKey"];
[defaults synchronize];
And the following code to read from the Settings.bundle:
NSUserDefaults *defaults2 = [NSUserDefaults standardUserDefaults];
int valueForSlider = [defaults2 integerForKey:@"TimerSliderKey"];
NSLog(@"The saved slider value is %@" , valueForSlider);
This code does not work. Can someone give me some replacement code that I could use?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的问题在这里:
您需要使用
%d
格式说明符,%@
是将description
消息发送到参数的格式说明符。以下是格式说明符列表:
请参阅 字符串编程指南了解更多信息。
Your problem is here:
You need to use the
%d
format specifier,%@
is the format specifier to send thedescription
message to the argument.Here's a list of format specifiers:
See the String Programming Guide for more information.
另一个(也许是真正的)问题是 UISlider 的默认值范围是 0.0 到 1.0。将其存储为整数时,由于截断,您只能看到 0 或 1。
您应该将其存储为浮点,而不是完全保留滑块的值。
The other, and perhaps the real, problem is that UISlider's value ranges from 0.0 to 1.0 by default. Storing it as an integer, you will only ever see 0 or 1 due to truncation.
You should store it as a floating point instead to fully preserve the value of the slider.
使用这个...
use this one...