iOS App

iOS App - Settings Bundle



这里延续上一篇(用InterfaceBuilder介绍...),继续介绍Settings Bundle

环境:XCode 4.3
2012 02 29 11 52 52
新增Settings Bundle,并命名为Settings.bundle
2012 02 29 11 58 07
2012 02 29 12 39 04
完成后,可以直接测试[Home键->设定->SimpleAppDelegate](参考文章一开始的图片)
2012 02 29 12 43 27
接下来,要自己动手修改内容之前,先看一下支持的对象类
2012 02 29 16 26 34
※在Settings.Bundle的摆放先后顺序会影响呈现时候的效果,可以用剪下、贴上的方式进行移动
对象设定属性
2012 02 29 17 10 31
画面Layout
2012 03 01 19 23 58
.H程序

#import

@interface AppDelegate : UIResponder

{

UIWindow *window_;

}

@property (strong, nonatomic)IBOutlet UIWindow *window;

- (IBAction)buttonLoad:(id)sender;

- (IBAction)buttonSave:(id)sender;

@property (strong, nonatomic) IBOutlet UISegmentedControl *protocol;

@property (strong, nonatomic) IBOutlet UISlider *slider;

@property (strong, nonatomic) IBOutlet UITextField *txtVersion;

@property (strong, nonatomic) IBOutlet UISwitch *mySwitch;

@end

.M程序

#define kProtocol @"protocol"

#define kSlider @"slider"

#define kUserName @"userName"

#define kVersion @"version"

#define kSwitch @"switch"

//用来取得Settings.Bundle各对象的默认值

-(NSDictionary*)settingsBundleDefaultValues

{

NSMutableDictionary *defaultDic_ = [[NSMutableDictionary alloc]init];

NSURL *settingsUrl =

[NSURL fileURLWithPath:[[NSBundle mainBundle]

pathForResource:@"Root"

ofType:@"plist"

inDirectory:@"Settings.bundle"] isDirectory:YES];

NSDictionary *settingBundle = [NSDictionary dictionaryWithContentsOfURL:settingsUrl];

NSArray *preference_ = [settingBundle objectForKey:@"PreferenceSpecifiers"];

for (NSDictionary *component_ in preference_) {

NSString *key = [component_ objectForKey:@"Key"];

NSString *defaultValue = [component_ objectForKey:@"DefaultValue"];

if (!key||!defaultValue) continue;

if (![component_ objectForKey:key]) {

[defaultDic_ setObject:[component_ objectForKey:@"DefaultValue"] forKey:key];

}

}

return defaultDic_;

}

//读取

- (IBAction)buttonLoad:(id)sender {

/*

[protocol setTitle:@"SMTP" forSegmentAtIndex:0];

[protocol setTitle:@"HTTP" forSegmentAtIndex:1];

[protocol setTitle:@"IMAP" forSegmentAtIndex:2];

[protocol setTitle:@"POP3" forSegmentAtIndex:3];

*/

NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];

/*

在执行App之前必须进到"设定"去,去设定App的值

连settings.bundle内对各对象进行设定默认值也没有办法一开始就直接被读取

所以要对NSUserDefault的Key注册默认值,值的来源是Settings.Bundle的DefaultValue

*/

[userDefault registerDefaults:[self settingsBundleDefaultValues]];

NSArray *arrProtocol = [NSArray arrayWithObjects:@"SMTP",@"HTTP",@"IMAP",@"POP3", nil];

[protocol setSelectedSegmentIndex:[arrProtocol indexOfObject:[userDefault stringForKey:kProtocol]]];

txtVersion.text = [userDefault stringForKey:kVersion];

[mySwitch setOn:[userDefault boolForKey:kSwitch] animated:YES];

[slider setValue: [userDefault floatForKey:kSlider]];

}

//保存

- (IBAction)buttonSave:(id)sender {

NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];

NSArray *arrProtocol = [NSArray arrayWithObjects:@"SMTP",@"HTTP",@"IMAP",@"POP3", nil];

[userDefault setObject:[arrProtocol objectAtIndex:protocol.selectedSegmentIndex] forKey:kProtocol];

[userDefault setObject:txtVersion.text forKey:kVersion];

[userDefault setBool:mySwitch.on forKey:kSwitch];

[userDefault setFloat:slider.value forKey:kSlider];

}