文章转自 http://blog.youkuaiyun.com/iunion/article/details/7067544
1.首先创建iPhone应用工程

2.新建Settings Bundle文件
菜单->File->New->New File...

选择Settings Bundle创建文件,见上图
选中Root.plist并将Preference Items打开

这个编辑器很不好用,建议使用Source Code来编辑,在Root.plist文件上按鼠标右键,如下图

3.编辑Root.plist
具体的编辑就不写了,主要需要注意
“Default Value”项最好填写上,作为默认值初始化用
4.编写程序
首先修改ViewController.h
- #import <UIKit/UIKit.h>
- @interface ViewController : UIViewController
- {
- IBOutlet UILabel *lblTite;
- IBOutlet UILabel *lblText;
- IBOutlet UILabel *lblToogle;
- IBOutlet UILabel *lblSlider;
- IBOutlet UILabel *lblBackground;
- }
- @property (nonatomic, retain) UILabel *lblTite;
- @property (nonatomic, retain) UILabel *lblText;
- @property (nonatomic, retain) UILabel *lblSlider;
- @property (nonatomic, retain) UILabel *lblToogle;
- @property (nonatomic, retain) UILabel *lblBackground;
- @end
修改ViewController.m
- @synthesize lblTite,lblText,lblToogle,lblSlider,lblBackground;
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- // Do any additional setup after loading the view, typically from a nib.
- NSString *titeValue = [[NSUserDefaults standardUserDefaults] stringForKey:@"title_preference"];
- NSString *textValue = [[NSUserDefaults standardUserDefaults] stringForKey:@"text_preference"];
- NSString *toogleValue = [[NSUserDefaults standardUserDefaults] stringForKey:@"enabled_preference"];
- NSString *sliderValue = [[NSUserDefaults standardUserDefaults] stringForKey:@"slider_preference"];
- NSString *backgroundValue = [[NSUserDefaults standardUserDefaults] stringForKey:@"backgroud_preference"];
- lblTite.text = [NSString stringWithFormat:@"Title Value: %@", titeValue];
- lblText.text = [NSString stringWithFormat:@"Text Value: %@", textValue];
- lblToogle.text = [NSString stringWithFormat:@"Toggle Control Value: %@", toogleValue];
- lblSlider.text = [NSString stringWithFormat:@"Slider Value: %@", sliderValue];
- lblBackground.text = [NSString stringWithFormat:@"Selected color value: %@", backgroundValue];
- }
修改AppDelegate.m
- // 获取默认设置
- - (void)registerDefaultsFromSettingsBundle
- {
- NSString *settingsBundle = [[NSBundle mainBundle] pathForResource:@"Settings" ofType:@"bundle"];
- if(!settingsBundle) {
- NSLog(@"Could not find Settings.bundle");
- return;
- }
- NSDictionary *settings = [NSDictionary dictionaryWithContentsOfFile:[settingsBundle stringByAppendingPathComponent:@"Root.plist"]];
- NSArray *preferences = [settings objectForKey:@"PreferenceSpecifiers"];
- NSMutableDictionary *defaultsToRegister = [[NSMutableDictionary alloc] initWithCapacity:[preferences count]];
- for(NSDictionary *prefSpecification in preferences) {
- NSString *key = [prefSpecification objectForKey:@"Key"];
- if(key) {
- [defaultsToRegister setObject:[prefSpecification objectForKey:@"DefaultValue"] forKey:key];
- }
- }
- [[NSUserDefaults standardUserDefaults] registerDefaults:defaultsToRegister];
- //[defaultsToRegister release];
- }
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- {
- // Override point for customization after application launch.
- NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
- NSString *title = [defaults stringForKey:@"title_preference"];
- if(!title) {
- // 加载默认配置
- [self performSelector:@selector(registerDefaultsFromSettingsBundle)];
- }
- return YES;
- }
例子下载:BundleSettings.zip
本文详细介绍如何在iOS应用中创建并使用Settings Bundle,包括步骤说明、代码实现及如何读取设置中的值更新UI。
1498

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



