- What Is Application Settings
It is used to let you add settings of yourown application to Apple application Settings. What you need do is to prepare a settings bundle, then Apple application Settings will create user interfaces for you. Settings bundle is a group of files built into your application that tells the Apple application Settings what preferences your application wishes to collect from user, settings bundle is in the format of property list.
The root level preferences view must be defined in Root.plist, if you need addition sub views, you must create new property list file (its name can't be Root.plist), and add entry in Root.plist for each sub view.
Click Settings icon on iPhone and iPod Touch to launch Settings. - Create Settings Bundle File
- Click project name on Group & Files pane in XCode
- Click menu File > New File...
- In the left pane, select Settings under the iPhone OS heading, then select Settings Bundle icon
- Click Next, and choose the default name of Settings. bundle by press Return, but if you want to create settings bundle for sub view, you must change the name.
- Now, a new item named Settings.bunder is generated on Group & Files pane in XCode, expand it, you will find 2 items, Root.plist, and a folder named en.lproj.
- ...
- Settings Bundle Format
- Root.plist
Root node, outmost node, it contains must children:
- Title(String), the title for our application will display on Settings application
- StringsTable(String), optional.
- PreferenceSpecifiers (Array), define available setting items, it holds a set of dictionary nodes (preference node), each of them represents a single preference that the user can enter, or a single child view that the user can drill down into. When you add a new sub node under PreferenceSpecifiers node, the default type is String, you must change it to Dictionary.
- Property List For Sub View
- Preference Node Format
The performance node contains several sub nodes, the data type of these sub nodes can be Boolean, Date, Data, Number, String, integer...... - Common sub nodes
- Type (String), Sepcify the type of preference node
- Title (STring), Specify the visible label for the preference node
- Key (String), Specify the key used to store the value of this preference item and retrive the value later
- Start a new group
- Type -> PSGroupSpecifier
- Text Field
- Type (String) -> PSTextFieldSpecifier
- AutoCapitalizationType (String) -> None or other value, means whether to autocapitalize what the user types in
- AutoCorrectionType (String) -> Yes or No, mean whether to autocorrect values entered into this text field.
- Secutity Text Field
The same as Text Field, but need to add addition sub node:
- IsSecure (Boolean) -> Check the checkbox at the end of this row
- Multivalue Field
Add a disclosure indicator in this row, click it will take ou down to a tab view to ler you select one of several rows.
- Type (String) -> PSMultiValueSpecifier
- Values (Array) -> Hold a list of values that actually get stored in the User Defaults, you need to add sub items under this nodes
- Titles (Array) -> Hold a list of values that user can select from, you need to add the same account of sub items under this nodes as node Values. If user select the first title, the first value is stored.
- Toggle Switch
- Type (String) -> PSToggleSwitchSpecifier
- TrueValue/FalseValue -> By default, the value of this control can be YES or NO for on/off state. But you can define yourown value to represent on/off state by define sub node TrueValue and FalseValue, the type of these node can be String, Number, Date.
- DefaultValue -> If you don't define TrueValue and FalseValue, the type of this node must be Boolean; Otherwise, change the type of it to the same as TrueValue/FalseValue, and set value to that defined in node TrueValue/FalseValue, - Slider
- Type (String) > PSSliderSpecifier
- DefaultValue (Number) -> Specify default value for the slider
- MinimumValue (Number) -> Specify the available minimum value for the slider
- MaximumValue Number) -> Specify the available maximum value for the slider
- MinimumValueImage (String) -> Specify a file name of image to display on minimum end of slider, this node is optional
- MaximumValueImage (String) -> Specify a file name of image to display on maximum end of slider, this node is optional
- Child Setting View
Tell Settings application that we want to diaplay a child settings view. This specifier will present a row with disclosure indicator, when typped, wil take the user down to a whole new view full of preferences.
- Type (String) -> PSChildPaneSpecifier
- File (String) -> Specify base name of property list file, such as MySubView for MySubView.plist. You need to create a new setting bundle for the sub view.
- ...
- ...
- Read & Change Settings Using NSUserDefaults
- Initial
NSUserDefaults *defaults = [NSUSerDefaults standardUserDefaults]; - Read Settings
- Get Object, like NSString, NSDate, NSData
[defaults objectForKey:@"xxxxxx"]; //The key is that defined in settings bundle file
- Get Scalar, like int, float, boolean
[defaults intForKey:@"xxx"];
[defaults floatForKey:@"xxx"];
[defaults booleanForKey:@"xxx"];
....
- Shcnage Settings
- Set Object, like NSString, NSDate, NSData
[defaults setObject:xxxx forKey:@"yyyy"]; //xxxx is value for key
- Set Scalar, like int, float, boolean
[defaults setInt:xxx forKey:@"yyyy"];
[defaults setFloat:xxx forKey:@"yyyy"];
[defaults setBoolean:xxx forKey:@"yyyy"];
....
- ...
- Advanced
More details, please refere to Settings Application Schema Reference - ...
- ...
Reference:- Beginning iPhone Development: Exploring the iPhone SDK
Chapter: Application Settings and User Defaults
Page: P304~327 - ...
阅读(1574) | 评论(0) | 转发(0) |