iOS App - Settings Bundle

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 <UIKit/UIKit.h>

 

@interface AppDelegate : UIResponder <UIApplicationDelegate>

{

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

所以要對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];

}