Settings Bundle -- iPhone应用设置简单分析

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




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值