Settings.Bundle操作

#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內對各物件進行設定預設值也沒有辦法一開始就直接被讀取

所以要對NSUserDefaultKey註冊預設值,值的來源是Settings.BundleDefaultValue

*/

[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];

}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值