xcode 在 iPhone 应用程序中访问 Settings.bundle 的 Child.plist - 如何?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2316095/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 19:08:20  来源:igfitidea点击:

Accessing Child.plist of Settings.bundle in iPhone application - How?

iphonexcodeplistsettings.bundle

提问by Sagar R. Kothari

I am facing a problem in accessing Settings.bundle.

我在访问 Settings.bundle 时遇到问题。

Let me explain what exactly I am doing.

让我解释一下我到底在做什么。

  1. Any kind of new Project Template from File ( Under iPhone OS ) - preferable View based.
  2. Now Right click on your project / Resources group.
  3. Select Add / New File
  4. Under iPhone Os - resource - Settings Bundle.
  5. Name it as strictly "Settings.bundle"
  1. 来自文件的任何类型的新项目模板(在 iPhone 操作系统下) - 基于视图的首选。
  2. 现在右键单击您的项目/资源组。
  3. 选择添加/新建文件
  4. 在 iPhone Os - 资源 - 设置包下。
  5. 将其严格命名为“Settings.bundle”

Now The very important steps.

现在 非常重要的步骤。

  1. Expand Settings.bundle in your project.
  2. Click on Root.plist
  3. In Editor - Expand Preference Specifiers
  4. Remove Item 2, Item 3, Item 4
  5. Select Item 1 & copy & Past it.
  6. Edit Item 2' type ( Item 1's copy )
  7. Set PSChildPaneSpecifieras type ( of item'2 )
  8. Set Title as you wish.
  9. Add a new Key value pair to this ( item 2 ) dictionary.
  10. Set Key = "File" & String = "FontSettings"
  1. 在您的项目中展开 Settings.bundle。
  2. 单击 Root.plist
  3. 在编辑器中 - 展开首选项说明符
  4. 删除项目 2、项目 3、项目 4
  5. 选择项目 1 并复制并粘贴它。
  6. 编辑项目 2' 类型(项目 1 的副本)
  7. PSChildPaneSpecifier设置为类型( item'2 )
  8. 根据需要设置标题。
  9. 向这个(第 2 项)字典添加一个新的键值对。
  10. 设置键 = "文件" & 字符串 = "字体设置"

Now In above steps, I have specified " I need a child pane for settings " & Child Pane settings will be loaded from FontSettings.plist

现在在上面的步骤中,我已经指定了“我需要一个用于设置的子窗格”并且子窗格设置将从FontSettings.plist加载

Ok. Further Steps.

好的。进一步的步骤。

  1. Now, Right click on your Settings.bundle / en.lproj ( Just because we need to add another plist file under settings.bundle.
  2. Add / New File - Under Mac OS x - Choose Other -> Property List
  3. Name as FontSettings.plist
  1. 现在,右键单击您的 Settings.bundle / en.lproj (只是因为我们需要在 settings.bundle 下添加另一个 plist 文件。
  2. 添加/新建文件 - 在 Mac OS x 下 - 选择其他 -> 属性列表
  3. 命名为FontSettings.plist

For just verification that settings.bundle contains that file or not.

仅用于验证 settings.bundle 是否包含该文件。

  1. Open your project in finder.
  2. Right click on Settings.bundle
  3. Click on show package contents.
  4. If it shows FontSettings.plistin package contents, Then it's perfect till now.
  1. 在 finder 中打开您的项目。
  2. 右键单击Settings.bundle
  3. 单击显示包内容。
  4. 如果它在包内容中显示FontSettings.plist,那么到目前为止它是完美的。

Now, For FontSettings.plist

现在,对于 FontSettings.plist

  1. Open Root.plist under settings.bundle.
  2. Copy Stringtable, PreferenceSpecifiers to the FontSettings.plist
  3. Now in FontSettings.plist- Add settings as you wish.
  1. 在 settings.bundle 下打开 Root.plist。
  2. 将 Stringtable、PreferenceSpecifiers 复制到 FontSettings.plist
  3. 现在在FontSettings.plist- 根据需要添加设置。

Check in simulator.

进入模拟器。

  1. Run program & tap on button. ( To terminate app. )
  2. Go to settings.
  3. Open Your Project Settings
  4. You can go to child pane.
  1. 运行程序并点击按钮。(终止应用程序。)
  2. 前往设置。
  3. 打开您的项目设置
  4. 您可以转到子窗格。


Till now, I believe I have followed the correct steps.

到目前为止,我相信我已经遵循了正确的步骤。

The problem that I am facing is here now.

我现在面临的问题就在这里。

If there is just a single Root.plist, we can directly access

如果只有一个Root.plist,我们可以直接访问

[[NSUserDefaults standardUserDefaults] stringForKey:@"fName"]

Like above.

像上面那样。

But the question is "How to access - child pane settings ? "

但问题是“如何访问子窗格设置?”

I have tried following way

我试过以下方式

NSString *str=[[NSBundle mainBundle] pathForResource:@"Settings" ofType:@"bundle"];
NSString *path=[str stringByAppendingPathComponent:@"FontSettings.plist"];
NSDictionary *dFor=[NSDictionary dictionaryWithContentsOfFile:path];

But the problem in above code is "It's just giving me the same default value every time, even after - changing the values from settings of iPhone - settings.

但上面代码中的问题是“它只是每次都给我相同的默认值,即使在 - 从 iPhone 的设置中更改值 - 设置。

What should I do for Accessing Child.plist settings? (that user has set).

我应该怎么做访问 Child.plist 设置?(该用户已设置)。

回答by kennytm

You access child pane settings exactly like the root settings. There's no difference between hierarchies in accessing the user defaults.

您可以像访问根设置一样访问子窗格设置。访问用户默认值的层次结构之间没有区别。

For instance, if a PSToggleSwitchSpecifier in FontSettings.plist has Keybeing isBold, then you'll access this setting with

例如,如果 FontSettings.plist 中的 PSToggleSwitchSpecifierKeyisBold,那么您将使用

[[NSUserDefaults standardUserDefaults] boolForKey:@"isBold"]