1、与工程中添加Resource->Setting Bundle
创建
文件说明:
en.lproj为多语言配置目录该目录的strings文件只有在IOS系统语言环境与文件夹想符才能读取。
Root.plist为Setting页面的根目录配置文件。
此时编译运行后已经能够看到通用设置中对应程序的Setting。
2、Setting页面制作Root.plist
Root.plist组成:
PreferenceSpecifiers 包含Setting页面中的子项
Strings Filename 指定多语言环境下的strings文件对应语言环境生效
StringsTable 指定strings文件直接生效
以下是一个Setting设置plist例子
Root.plist
[html] view plain copy
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>PreferenceSpecifiers</key>
<array>
<dict>
<key>Type</key>
<string>PSGroupSpecifier</string>
<key>Title</key>
<string>Sound</string>
</dict>
<dict>
<key>Type</key>
<string>PSToggleSwitchSpecifier</string>
<key>Title</key>
<string>Play Sound</string>
<key>Key</key>
<string>play_sound_preference</string>
<key>DefaultValue</key>
<false/>
</dict>
<dict>
<key>Type</key>
<string>PSToggleSwitchSpecifier</string>
<key>Title</key>
<string>3D Sound</string>
<key>Key</key>
<string>3D_sound_preference</string>
<key>DefaultValue</key>
<false/>
</dict>
<dict>
<key>Type</key>
<string>PSGroupSpecifier</string>
<key>Title</key>
<string>User Info</string>
</dict>
<dict>
<key>Type</key>
<string>PSTextFieldSpecifier</string>
<key>Title</key>
<string>Name</string>
<key>Key</key>
<string>user_name</string>
</dict>
<dict>
<key>Type</key>
<string>PSMultiValueSpecifier</string>
<key>Title</key>
<string>Experience Level</string>
<key>Key</key>
<string>experience_preference</string>
<key>DefaultValue</key>
<string>0</string>
<key>Titles</key>
<array>
<string>Beginner</string>
<string>Expert</string>
<string>Master</string>
</array>
<key>Values</key>
<array>
<string>0</string>
<string>1</string>
<string>2</string>
</array>
</dict>
<dict>
<key>Type</key>
<string>PSGroupSpecifier</string>
<key>Title</key>
<string>Gravity</string>
</dict>
<dict>
<key>Type</key>
<string>PSSliderSpecifier</string>
<key>Key</key>
<string>gravity_preference</string>
<key>DefaultValue</key>
<integer>1</integer>
<key>MinimumValue</key>
<integer>0</integer>
<key>MaximumValue</key>
<integer>2</integer>
</dict>
<dict>
<key>Type</key>
<string>PSGroupSpecifier</string>
<key>Title</key>
<string>Other</string>
</dict>
<dict>
<key>File</key>
<string>Other</string>
<key>Type</key>
<string>PSChildPaneSpecifier</string>
<key>Title</key>
<string>Other Setting</string>
<key>Key</key>
<string>other_setting</string>
</dict>
</array>
<key>Strings Filename</key>
<string>Root</string>
</dict>
</plist>
Other.plist(直接复制Root.plist更改Item的Key)
[html] view plain copy
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>PreferenceSpecifiers</key>
<array>
<dict>
<key>Type</key>
<string>PSGroupSpecifier</string>
<key>Title</key>
<string>Sound</string>
</dict>
<dict>
<key>Type</key>
<string>PSToggleSwitchSpecifier</string>
<key>Title</key>
<string>Play Sound</string>
<key>Key</key>
<string>oplay_sound_preference</string>
<key>DefaultValue</key>
<false/>
</dict>
<dict>
<key>Type</key>
<string>PSToggleSwitchSpecifier</string>
<key>Title</key>
<string>3D Sound</string>
<key>Key</key>
<string>o3D_sound_preference</string>
<key>DefaultValue</key>
<false/>
</dict>
<dict>
<key>Type</key>
<string>PSGroupSpecifier</string>
<key>Title</key>
<string>User Info</string>
</dict>
<dict>
<key>Type</key>
<string>PSTextFieldSpecifier</string>
<key>Title</key>
<string>Name</string>
<key>Key</key>
<string>ouser_name</string>
</dict>
<dict>
<key>Type</key>
<string>PSMultiValueSpecifier</string>
<key>Title</key>
<string>Experience Level</string>
<key>Key</key>
<string>oexperience_preference</string>
<key>DefaultValue</key>
<string>0</string>
<key>Titles</key>
<array>
<string>Beginner</string>
<string>Expert</string>
<string>Master</string>
</array>
<key>Values</key>
<array>
<string>0</string>
<string>1</string>
<string>2</string>
</array>
</dict>
<dict>
<key>Type</key>
<string>PSGroupSpecifier</string>
<key>Title</key>
<string>Gravity</string>
</dict>
<dict>
<key>Type</key>
<string>PSSliderSpecifier</string>
<key>Key</key>
<string>ogravity_preference</string>
<key>DefaultValue</key>
<integer>1</integer>
<key>MinimumValue</key>
<integer>0</integer>
<key>MaximumValue</key>
<integer>2</integer>
</dict>
</array>
<key>Strings Filename</key>
<string>Root</string>
</dict>
</plist>
3、读取Setting配置
Setting是以Key Value的形式存储在NSUserDefaults中所以当Setting配置文件Key相同时会相互影响。
[cpp] view plain copy
NSUserDefaults *userdefaults = [NSUserDefaults standardUserDefaults];
//[userdefaults valueForKey:(NSString *)];//根据Key获取数据
Setting变更同步注册通知
[cpp] view plain copy
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(change) name:@"NSUserDefaultsDidChangeNotification" object:nil];