- Part 1 - Creating a settings bundle which will show up in settings.
In this tutorial, I will show you how to create Application Preferences which will show up in Settings. This is how the Application Preferences will look like
Application preferences are stored in a plist file. So lets start by adding a settings bundle which will give us the plist file we need. To follow the steps below you need a project opened in XCode.
Select your project file in XCode (left side, in the file explorer) and Click File -> New File -> Click on Settings -> Select Settings Bundle and click Next. Give it a name and click on Finish.
Expand Settings.bundle (which is what my bundle is named) and you will find a file called Root.plist where all of our preferences will be stored.
Click on Root.plist -> Expand Preference Specifiers. It is under Preference Specifiers, that all of our preferences will go. Some of them are already created for us. For the sake of this tutorial, I will delete all of them and create new ones from scratch.
Every field that shows up in Preferences is created by creating a new Item. This Item always has a Type and a Title.
Possible values for Type are
- PSTextFieldSpecifier
- PSTitleValueSpecifier
- PSToggleSwitchSpecifier
- PSSliderSpecifier
- PSMultiValueSpecifier
- PSGroupSpecifier
- PSChildPaneSpecifier
PSTextFieldSpecifier
Select "PreferenceSpecifiers" and click on the icon that shows up at the end. As shown in the image below. A random item number is shown in the image because I have deleted all of the items.
Set the Type to Dictionary and we will be able to customize how this item/field shows up. When you set it to Dictionary, we will be able to add sub items to it, as seen in the image below. Expand the Item and we will be able to customize this item.
Set the Title to "Text Entry", Key to "textEntry" and Type to "PSTextFieldSpecifier". With the help of the key, we will be able to get the value in our application. We will also set some default value. Create a new item under Item1 and name the key "DefaultValue" and set the Type to String with some default value in the value field. This is how the first item will look like.
We can further customize this field, by adding what type of keypad shows up, correction type, if it is secure or not and auto capitalization type.
Key: IsSecure
Type: Boolean
Value: YES/NO
Key: KeyboardType
Type: String
Value: Value must contain one of the following strings. Alphabet, NumbersAndPunctuation, NumberPad, URL and EmailAddress.
Key: AutocapitalizationType
Type: String
Value: Value must contain one of the following strings. None, Sentences, Words, AllCharacters. Default value is None.
Key: AutoCorrectionType
Type: String
Value: Value must contain one of the following strings. Default, No, Yes. Default value is Default.
PSTitleValueSpecifier
Key: Type (required)
Type: String
Value: PSTitleValueSpecifier
Key: Title (required)
Type: String
Value: Your string value.
Key: Key (required)
Type: String
Value: Your string value.
Key: DefaultValue (required)
Type: String
Value: Your string value.
Key: Values
Type: Array
Value: Key-Value entry
Key: Titles
Type: Array
Value: Key-Value entry
PSToggleSwitchSpecifier
Key: Type (required)
Type: String
Value: PSToogleSwitchSpecifier
Key: Title (required)
Type: String
Value: Your string value.
Key: Key (required)
Type: String
Value: Your string value.
Key: DefaultValue (required)
Type: String
Value: Your string value.
Key: TrueValue
Type: Boolean
Value: YES
Key: FalseValue
Type: Boolean
Value: NO
Key: Type (required)
Type: String
Value: PSSliderSpecifier
Key: Title (required)
Type: String
Value: Your string value.
Key: Key (required)
Type: String
Value: Your string value.
Key: DefaultValue (required)
Type: String
Value: Your string value.
Key: MinimumValue (required)
Type: Number
Value: Minimum number value here
Key: MaximumValue (required)
Type: Number
Value: Maximum number value here
Key: MinimumValueImage
Type: String
Value: Image (21*21)
Key: MaximumValueImage
Type: String
Value: Image (21*21)
Key: Type (required)
Type: String
Value: PSMultiValueSpecifier
Key: Title (required)
Type: String
Value: Your string value.
Key: Key (required)
Type: String
Value: Your string value.
Key: DefaultValue (required)
Type: String
Value: Your string value.
Key: Values
Type: Array
Value: Key-Value entry.
Key: Titles
Type: Array
Value: Key-Value entry.
Key: Type (required)
Type: String
Value: PSGroupSpecifier
Key: Title (required)
Type: String
Value: Your string value.
Key: Type (Required)
Type: String
Value: PSChildPaneSpecifier
Key: Title (required)
Type: String
Value: Your string value.
Key: File
Type: String
Value: The name of plist file, without the extension.
We need some way to read that data and display it on the screen. We do this with the help of the following code
NSString *readOnlyValue = [[NSUserDefaults standardUserDefaults] stringForKey:@"readOnly_key"];
NSString *sliderValue = [[NSUserDefaults standardUserDefaults] stringForKey:@"slider_key"];
NSString *colorValue = [[NSUserDefaults standardUserDefaults] stringForKey:@"colors_key"];
NSString *toogleValue = [[NSUserDefaults standardUserDefaults] stringForKey:@"toogle_key"];
When the application is loaded for the first time, the view is going to have all null values. It looks like even though we set the default value, we have to write some code to set the default values when the application loads. More about this in Part 2 of this tutorial.
In this tutorial, I have created a new view and a view controller. It is in that view I display all the data from the preferences.
You can download the source code here.
Please leave me your comments and let me know know what are your thoughts. Your support is always appreciated.
Source: http://www.iphonesdkarticles.com/2008/08/application-preferences.html
本文详细介绍了如何在iOS应用中创建并自定义偏好设置。通过使用Xcode中的Settings Bundle,可以实现包括文本输入、切换开关等多种类型的用户偏好选项。
835

被折叠的 条评论
为什么被折叠?



