Application Preferences

本文详细介绍了如何在iOS应用中创建并自定义偏好设置。通过使用Xcode中的Settings Bundle,可以实现包括文本输入、切换开关等多种类型的用户偏好选项。

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
  1. PSTextFieldSpecifier
  2. PSTitleValueSpecifier
  3. PSToggleSwitchSpecifier
  4. PSSliderSpecifier
  5. PSMultiValueSpecifier
  6. PSGroupSpecifier
  7. PSChildPaneSpecifier
Based on what type of specifier we set in the "Type" key , we have to supply its required keys.

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.

We would create different items in a similar way. Below is a list of all the keys, Types and values that an item can have.

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

If Values key is used then it should have a corresponding value in the Titles array.

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

PSSliderSpecifier

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)

PSMultiValueSpecifier

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.

PSGroupSpecifier

Key: Type (required)
Type: String
Value: PSGroupSpecifier

Key: Title (required)
Type: String
Value: Your string value.

PSChildPaneSpecifier

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.

After we have defined our schema file, we can test it. Click on Build and Go and click the Home Button, Click on Settings and you will see the name of your application there. Click on it to see all the details. If there is an error in the item, that item may not show up or the application will crash.

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 *textValue = [[NSUserDefaults standardUserDefaults] stringForKey:@"textEntry_key"];
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
### 问题分析与解决方法 在 Unity 或 3D 建模软件中无法打开 `Assets/shiyantai2.fbx` 文件的问题可能由多种原因引起。以下是一些常见的原因及解决方案: #### 1. **文件路径问题** 确保文件路径正确无误,Unity 的资源文件路径通常为 `Assets/` 下的子目录。如果路径不正确或文件未正确导入到 Unity 项目中,则可能导致文件无法被识别或打开。 #### 2. **FBX 文件格式不兼容** 某些 FBX 文件可能包含 Unity 不支持的功能或版本。例如,高版本的 FBX 文件可能无法被低版本的 Unity 支持。建议检查 FBX 文件的版本,并尝试将其导出为较低版本的 FBX 格式[^1]。 ```python # 使用 Autodesk® Maya® 或其他 3D 建模软件将 FBX 文件导出为较低版本 export FBX version: FBX 2014 ``` #### 3. **文件损坏** 如果 `shiyantai2.fbx` 文件在导出或传输过程中损坏,则可能导致其无法被打开。可以通过以下方式验证文件完整性: - 在原生 3D 建模软件(如 Autodesk® Maya® 或 Autodesk® 3ds Max®)中重新打开并保存该文件。 - 确保文件没有因压缩或传输而丢失数据。 #### 4. **外部应用偏好设置** 某些情况下,外部应用的偏好设置可能会影响 FBX 文件的正常加载。例如,3D 建模软件的导出设置可能需要调整以确保与 Unity 兼容。请检查以下设置: - 导出时启用 `ASCII` 或 `Binary` 模式。 - 确保单位和比例设置与 Unity 的场景一致[^1]。 #### 5. **Streaming Assets 路径问题** 如果文件位于 `StreamingAssets` 路径下(如 `/var/containers/Bundle/Application/app sandbox/xxx.app/Data/Raw`),则需要注意该路径是只读的,且需要通过代码动态加载文件[^2]。以下是加载 FBX 文件的示例代码: ```csharp using UnityEngine; using System.IO; public class LoadFBXFromStreamingAssets : MonoBehaviour { void Start() { string filePath = Path.Combine(Application.streamingAssetsPath, "shiyantai2.fbx"); if (File.Exists(filePath)) { // 加载 FBX 文件 GameObject fbxObject = Instantiate(Resources.Load("shiyantai2")) as GameObject; fbxObject.transform.position = Vector3.zero; } else { Debug.LogError("FBX file not found in StreamingAssets path."); } } } ``` #### 6. **Unity 版本兼容性** 确保使用的 Unity 版本支持当前 FBX 文件的格式。如果 Unity 版本较旧,可能需要升级至最新版本以获得更好的 FBX 支持。 --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值