创建IOS应用程序通用下的Setting以及读取方式

这篇博客介绍了如何在iOS应用中创建Setting Bundle,并详细解释了如何制作Root.plist文件来构建设置页面。内容包括多语言配置、Setting的Key Value存储方式以及Setting变更的同步通知注册。

相关资料:https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/UserDefaults/Introduction/Introduction.html

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文件直接生效


PreferenceSpecifiers包含的子项:

Preference control types

Control type

Description

Text field

The text field type displays a title (optional) and an editable text field. You can use this type for preferences that require the user to specify a custom string value.

The key for this type is PSTextFieldSpecifier.

Title

The title type displays a read-only string value. You can use this type to display read-only preference values. (If the preference contains cryptic or nonintuitive values, this type lets you map the possible values to custom strings.)

The key for this type is PSTitleValueSpecifier.

Toggle switch

The toggle switch type displays an ON/OFF toggle button. You can use this type to configure a preference that can have only one of two values. Although you typically use this type to represent preferences containing Boolean values, you can also use it with preferences containing non-Boolean values.

The key for this type is PSToggleSwitchSpecifier.

Slider

The slider type displays a slider control. You can use this type for a preference that represents a range of values. The value for this type is a real number whose minimum and maximum value you specify.

The key for this type is PSSliderSpecifier.

Multivalue

The multivalue type lets the user select one value from a list of values. You can use this type for a preference that supports a set of mutually exclusive values. The values can be of any type.

The key for this type is PSMultiValueSpecifier.

Group

The group type is for organizing groups of preferences on a single page. The group type does not represent a configurable preference. It simply contains a title string that is displayed immediately before one or more configurable preferences.

The key for this type is PSGroupSpecifier.

Child pane

The child pane type lets the user navigate to a new page of preferences. You use this type to implement hierarchical preferences. For more information on how you configure and use this preference type, see Hierarchical Preferences.

The key for this type is PSChildPaneSpecifier.


以下是一个Setting设置plist例子

Root.plist

<?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)

<?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相同时会相互影响。

NSUserDefaults *userdefaults = [NSUserDefaults standardUserDefaults];
//[userdefaults valueForKey:(NSString *)];//根据Key获取数据

Setting变更同步注册通知

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(change) name:@"NSUserDefaultsDidChangeNotification" object:nil];


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值