ios开发中 应用设置的简单开发 (Settings.bundle)

本文详细介绍如何在iOS应用中创建并使用Settings Bundle,包括步骤说明、代码实现及如何读取设置中的值更新UI。

文章转自  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

  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface ViewController : UIViewController  
  4. {  
  5.     IBOutlet UILabel *lblTite;  
  6.     IBOutlet UILabel *lblText;  
  7.     IBOutlet UILabel *lblToogle;  
  8.     IBOutlet UILabel *lblSlider;  
  9.     IBOutlet UILabel *lblBackground;  
  10. }  
  11.   
  12. @property (nonatomic, retain) UILabel *lblTite;  
  13. @property (nonatomic, retain) UILabel *lblText;  
  14. @property (nonatomic, retain) UILabel *lblSlider;  
  15. @property (nonatomic, retain) UILabel *lblToogle;  
  16. @property (nonatomic, retain) UILabel *lblBackground;  
  17.   
  18. @end  


修改ViewController.m

  1. @synthesize lblTite,lblText,lblToogle,lblSlider,lblBackground;  
  2.   
  3.   
  4. - (void)viewDidLoad  
  5. {  
  6.     [super viewDidLoad];  
  7.     // Do any additional setup after loading the view, typically from a nib.  
  8.   
  9.     NSString *titeValue = [[NSUserDefaults standardUserDefaults] stringForKey:@"title_preference"];  
  10.     NSString *textValue = [[NSUserDefaults standardUserDefaults] stringForKey:@"text_preference"];  
  11.     NSString *toogleValue = [[NSUserDefaults standardUserDefaults] stringForKey:@"enabled_preference"];  
  12.     NSString *sliderValue = [[NSUserDefaults standardUserDefaults] stringForKey:@"slider_preference"];  
  13.     NSString *backgroundValue = [[NSUserDefaults standardUserDefaults] stringForKey:@"backgroud_preference"];  
  14.       
  15.     lblTite.text = [NSString stringWithFormat:@"Title Value: %@", titeValue];  
  16.     lblText.text = [NSString stringWithFormat:@"Text Value: %@", textValue];  
  17.     lblToogle.text = [NSString stringWithFormat:@"Toggle Control Value: %@", toogleValue];  
  18.     lblSlider.text = [NSString stringWithFormat:@"Slider Value: %@", sliderValue];  
  19.     lblBackground.text = [NSString stringWithFormat:@"Selected color value: %@", backgroundValue];  
  20. }  


修改AppDelegate.m

  1. // 获取默认设置  
  2. - (void)registerDefaultsFromSettingsBundle  
  3. {  
  4.     NSString *settingsBundle = [[NSBundle mainBundle] pathForResource:@"Settings" ofType:@"bundle"];  
  5.     if(!settingsBundle) {  
  6.         NSLog(@"Could not find Settings.bundle");  
  7.         return;  
  8.     }  
  9.       
  10.     NSDictionary *settings = [NSDictionary dictionaryWithContentsOfFile:[settingsBundle stringByAppendingPathComponent:@"Root.plist"]];  
  11.     NSArray *preferences = [settings objectForKey:@"PreferenceSpecifiers"];  
  12.       
  13.     NSMutableDictionary *defaultsToRegister = [[NSMutableDictionary alloc] initWithCapacity:[preferences count]];  
  14.     for(NSDictionary *prefSpecification in preferences) {  
  15.         NSString *key = [prefSpecification objectForKey:@"Key"];  
  16.         if(key) {  
  17.             [defaultsToRegister setObject:[prefSpecification objectForKey:@"DefaultValue"] forKey:key];  
  18.         }  
  19.     }  
  20.     [[NSUserDefaults standardUserDefaults] registerDefaults:defaultsToRegister];  
  21.     //[defaultsToRegister release];  
  22. }  
  23.   
  24.   
  25. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
  26. {  
  27.     // Override point for customization after application launch.  
  28.   
  29.     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];  
  30.     NSString *title = [defaults stringForKey:@"title_preference"];  
  31.     if(!title) {  
  32.         // 加载默认配置  
  33.         [self performSelector:@selector(registerDefaultsFromSettingsBundle)];          
  34.     }  
  35.     return YES;  
  36. }  


例子下载:BundleSettings.zip


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值