简体   繁体   English

无法设置NSUserDefault ToggleSwitch值

[英]Cannot set NSUserDefault ToggleSwitch Value

I have a form within my IPad app that allows the user to configure the application settings (they can also change these settings from the IPad settings app). 我的IPad应用程序中有一个表单,允许用户配置应用程序设置(他们也可以从IPad设置应用程序更改这些设置)。 I have the following code: 我有以下代码:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setValue:externalAddress forKey:@"firstString_preference"];
[defaults setValue:internalAddress forKey:@"secondString_perference"];
[defaults setValue:isConnectedToDemoString forKey:@"firstToggle_preference"];
[defaults setValue:isConnectedToInternal forKey:@"secondToggle_preference"];
[defaults synchronize];

The firstString_preference and secondString_perference are Textfields in the settings bundle and they save to the settings without issue. firstString_preference和secondString_perference是设置包中的Textfields,它们可以毫无问题地保存到设置中。 My problem is the firstToggle_preference and secondToggle_preference are toggle switches in the settings bundle and I cannot seem to set these at all. 我的问题是firstToggle_preference和secondToggle_preference是设置包中的切换开关,我似乎根本无法设置这些。 They always seem to be set to no. 他们似乎总是设置为否。

Does anyone know what I am doing wrong? 有谁知道我做错了什么? Should i be using a different method for setting Toggle Switch default values? 我应该使用不同的方法来设置Toggle Switch的默认值吗?

Thanks in advance 提前致谢

What type are isConnectedToDemoString and isConnectedToInternal . 什么类型是isConnectedToDemoStringisConnectedToInternal Unless those are NSNumber s, you should use setBool:forKey: . 除非那些是NSNumber ,否则你应该使用setBool:forKey: .

Also, while setValue:forKey: works for storing object types to the user defaults, your code would be clearer if you used the NSUserDefaults -native setObject:forKey: method. 此外,虽然setValue:forKey:用于将对象类型存储到用户默认值,但如果使用NSUserDefaults setObject:forKey:方法,则代码会更清晰。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM