在Android手机上, 在某个程序里,通过按Menu键,一般都会打开这个程序的设置,而在iOS里,系统提供了一个很好的保存程序设置的机制。就是使用Settings Bundle。
在按了HOME键的情况下,在第一页的图标中找到设置,会看到程序的设置都在这里。那如何添加自己的程序的设置项呢?
1、添加设置项
默认情况下,新建的项目程序是没有设置项的。新建一个项目,命名为 SettingsBundleDemo,选择Single View App模版创建。项目创建完成,在项目里选择创建新文件,
选择Resource 中的Settings Bundle,创建。
再给程序添加一个icon。运行。按home键,打开设置,看到设置里多了一项,SettingsBundleDemo。这就为程序添加了一个设置。
2、设置的控件
默认的生成的设置项里有这个几个控件。
分别是:Group分组,文本框,Slider,开关控件几个控件。
设置想能使用的控件如下:
设置控件 | 类型 |
---|---|
文本框 | PSTextFieldSpecifier |
文字 | PSTitleValueSpecifier |
开关控件 | PSToggleSwitchSpecifier |
Slider | PSSliderSpecifier |
Multivalue | PSMultiValueSpecifier |
Group | PSGroupSpecifier |
子面板 |
PSChildPaneSpecifier . |
3、编辑设置项的文件
展开Settings.bundle,其中包含一个Root.plist。Settings程序中的显示项就是从Root.plist中获取的。单击Root.plist以打开它,在空白处单击,选中Show Raw Keys/Values:
我们把原有的项删掉,添加自己的设置项,添加如下:
对应的plist源文件是这样的:如果你觉得自己手工输入这些项很慢,可以把下面的源文件拷贝到Root.plist里,用源代码打开方式就可以编辑了。
- <?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>
这时候运行,在来到设置项看:
已经是我们自己设置的效果了。
4、在程序中获取Settings 和写入Settings 添加UI
这里的项目是设置好了,那怎么读取呢?我们先在程序里添加一些对应的UI.打开.xib文件,往里放置控件,并生成对应的映射和Action。
pickerView的使用请参考iOS学习之UIPickerView控件的简单使用这篇文章。
5、实现读取设置和保存代码
关键是通过: NSUserDefaults *defaults = [NSUserDefaultsstandardUserDefaults];
代码获取设置项的NSUserDefaults值,然后通过key获取设置的内容和保存设置内容
在两个Button的按下事件实现如下:
- - (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];
- }
头文件实现:
- #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文件中其他代码:
- #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 和爱好 足球,选择等级,保存设置。打开设置查看,可以读取到保存后的设置。
这样就可以操作和这只程序的设置项了。