在Android手機上, 在某個程序裏,經過按Menu鍵,通常都會打開這個程序的設置,而在iOS裏,系統提供了一個很好的保存程序設置的機制。就是使用Settings Bundle。html
在按了HOME鍵的狀況下,在第一頁的圖標中找到設置,會看到程序的設置都在這裏。那如何添加本身的程序的設置項呢?app
默認狀況下,新建的項目程序是沒有設置項的。新建一個項目,命名爲 SettingsBundleDemo,選擇Single View App模版建立。項目建立完成,在項目裏選擇建立新文件,ide
選擇Resource 中的Settings Bundle,建立。學習
再給程序添加一個icon。運行。按home鍵,打開設置,看到設置裏多了一項,SettingsBundleDemo。這就爲程序添加了一個設置。
atom
默認的生成的設置項裏有這個幾個控件。spa
分別是:Group分組,文本框,Slider,開關控件幾個控件。.net
設置想能使用的控件以下:code
設置控件 | 類型 |
---|---|
文本框 | PSTextFieldSpecifier |
文字 | PSTitleValueSpecifier |
開關控件 | PSToggleSwitchSpecifier |
Slider | PSSliderSpecifier |
Multivalue | PSMultiValueSpecifier |
Group | PSGroupSpecifier |
子面板 | PSChildPaneSpecifier . |
展開Settings.bundle,其中包含一個Root.plist。Settings程序中的顯示項就是從Root.plist中獲取的。單擊Root.plist以打開它,在空白處單擊,選中Show Raw Keys/Values:
component
咱們把原有的項刪掉,添加本身的設置項,添加以下:orm
對應的plist源文件是這樣的:若是你以爲本身手工輸入這些項很慢,能夠把下面的源文件拷貝到Root.plist裏,用源代碼打開方式就能夠編輯了。
[html] view plaincopy
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>PreferenceSpecifiers</key>
<array>
<dict>
<key>Type</key>
<string>PSGroupSpecifier</string>
<key>Title</key>
<string>我的信息</string>
<key>Key</key>
<string></string>
</dict>
<dict>
<key>Type</key>
<string>PSTextFieldSpecifier</string>
<key>Title</key>
<string>姓名</string>
<key>Key</key>
<string>username</string>
</dict>
<dict>
<key>Type</key>
<string>PSMultiValueSpecifier</string>
<key>Values</key>
<array>
<string>football</string>
<string>basketball</string>
<string>pingpong</string>
</array>
<key>Title</key>
<string>愛好</string>
<key>Titles</key>
<array>
<string>足球</string>
<string>籃球</string>
<string>乒乓球</string>
</array>
<key>Key</key>
<string>aihao</string>
<key>DefaultValue</key>
<string>football</string>
</dict>
<dict>
<key>FalseValue</key>
<string>NO</string>
<key>TrueValue</key>
<true/>
<key>DefaultValue</key>
<false/>
<key>Type</key>
<string>PSToggleSwitchSpecifier</string>
<key>Title</key>
<string>婚姻情況</string>
<key>Key</key>
<string>maritalStatus</string>
</dict>
<dict>
<key>Type</key>
<string>PSGroupSpecifier</string>
<key>Title</key>
<string>等級</string>
<key>Key</key>
<string></string>
</dict>
<dict>
<key>DefaultValue</key>
<integer>5</integer>
<key>MaximumValue</key>
<integer>10</integer>
<key>MinimumValue</key>
<integer>1</integer>
<key>Type</key>
<string>PSSliderSpecifier</string>
<key>Title</key>
<string>等級</string>
<key>Key</key>
<string>levelState</string>
</dict>
</array>
<key>StringsTable</key>
<string>Root</string>
</dict>
</plist>
這時候運行,在來到設置項看:
已是咱們本身設置的效果了。
這裏的項目是設置好了,那怎麼讀取呢?咱們先在程序裏添加一些對應的UI.打開.xib文件,往裏放置控件,並生成對應的映射和Action。
pickerView的使用請參考iOS學習之UIPickerView控件的簡單使用這篇文章。
關鍵是經過: NSUserDefaults *defaults = [NSUserDefaultsstandardUserDefaults];
代碼獲取設置項的NSUserDefaults值,而後經過key獲取設置的內容和保存設置內容
在兩個Button的按下事件實現以下:
[cpp] view plaincopy
- (IBAction)getSettings:(id)sender {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
username.text = [defaults objectForKey:@"username"];
selectedAihao = [defaults objectForKey:@"aihao"];
NSLog(@"aihao:%@",selectedAihao);
NSInteger aihaoIndex = [aihaoValues indexOfObject:selectedAihao];
[pickerView selectRow:aihaoIndex inComponent:0 animated:YES];
[level setValue:[defaults integerForKey:@"levelState"]];
}
- (IBAction)setSettings:(id)sender {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setValue:username.text forKey:@"username"];
NSInteger aihaoIndex = [aihaoTitles indexOfObject:selectedAihao];
[defaults setValue:[aihaoValues objectAtIndex:aihaoIndex] forKey:@"aihao"];
[defaults setInteger:level.value forKey:@"levelState"];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"偏好設置"
message:@"偏好設置已經保存!"
delegate:nil
cancelButtonTitle: @"完成"
otherButtonTitles:nil];
[alert show];
}
頭文件實現:
[cpp] view plaincopy
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UIPickerViewDelegate, UIPickerViewDataSource>
{
NSMutableArray *aihaoTitles;
NSMutableArray *aihaoValues;
NSString *selectedAihao;
}
@property (strong, nonatomic) IBOutlet UITextField *username;
@property (strong, nonatomic) IBOutlet UIPickerView *pickerView;
@property (strong, nonatomic) IBOutlet UISlider *level;
- (IBAction)getSettings:(id)sender;
- (IBAction)setSettings:(id)sender;
- (IBAction)doneEdit:(id)sender;
@end
.m文件中其餘代碼:
[cpp] view plaincopy
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize username;
@synthesize pickerView;
@synthesize level;
- (void)viewDidLoad
{
[super viewDidLoad];
aihaoTitles = [[NSMutableArray alloc] init];
[aihaoTitles addObject:@"足球"];
[aihaoTitles addObject:@"籃球"];
[aihaoTitles addObject:@"乒乓球"];
aihaoValues = [[NSMutableArray alloc] init];
[aihaoValues addObject:@"football"];
[aihaoValues addObject:@"basketball"];
[aihaoValues addObject:@"pingpong"];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[self setUsername:nil];
[self setPickerView:nil];
[self setLevel:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
-(NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
-(NSInteger) pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
{
return [aihaoTitles count];
}
-(NSString *) pickerView:(UIPickerView *)pickerView
titleForRow:(NSInteger)row
forComponent:(NSInteger)component
{
return [aihaoTitles objectAtIndex:row];
}
-(void) pickerView:(UIPickerView *)pickerView
didSelectRow:(NSInteger)row
inComponent:(NSInteger)component
{
selectedAihao = [aihaoTitles objectAtIndex:row];
}
- (IBAction)doneEdit:(id)sender{
}
運行,輸入姓名zhongguo 和愛好 足球,選擇等級,保存設置。打開設置查看,能夠讀取到保存後的設置。
這樣就能夠操做和這隻程序的設置項了。
例子代碼:http://download.csdn.net/detail/totogo2010/4398462
著做權聲明:本文由http://blog.csdn.net/totogo2010/原創,歡迎轉載分享。請尊重做者勞動,轉載時保留該聲明和做者博客連接,謝謝
謝謝這篇文章的做者,講解的很詳細,頗有用