iOS开发之iOS程序偏好设置(Settings Bundle)的使用

目录[-]

在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里,用源代码打开方式就可以编辑了。

[html] view plaincopy

  1. <?xml version="1.0" encoding="UTF-8"?>  

  2. <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">  

  3. <plist version="1.0">  

  4. <dict>  

  5.     <key>PreferenceSpecifiers</key>  

  6.     <array>  

  7.         <dict>  

  8.             <key>Type</key>  

  9.             <string>PSGroupSpecifier</string>  

  10.             <key>Title</key>  

  11.             <string>个人信息</string>  

  12.             <key>Key</key>  

  13.             <string></string>  

  14.         </dict>  

  15.         <dict>  

  16.             <key>Type</key>  

  17.             <string>PSTextFieldSpecifier</string>  

  18.             <key>Title</key>  

  19.             <string>姓名</string>  

  20.             <key>Key</key>  

  21.             <string>username</string>  

  22.         </dict>  

  23.         <dict>  

  24.             <key>Type</key>  

  25.             <string>PSMultiValueSpecifier</string>  

  26.             <key>Values</key>  

  27.             <array>  

  28.                 <string>football</string>  

  29.                 <string>basketball</string>  

  30.                 <string>pingpong</string>  

  31.             </array>  

  32.             <key>Title</key>  

  33.             <string>爱好</string>  

  34.             <key>Titles</key>  

  35.             <array>  

  36.                 <string>足球</string>  

  37.                 <string>篮球</string>  

  38.                 <string>乒乓球</string>  

  39.             </array>  

  40.             <key>Key</key>  

  41.             <string>aihao</string>  

  42.             <key>DefaultValue</key>  

  43.             <string>football</string>  

  44.         </dict>  

  45.         <dict>  

  46.             <key>FalseValue</key>  

  47.             <string>NO</string>  

  48.             <key>TrueValue</key>  

  49.             <true/>  

  50.             <key>DefaultValue</key>  

  51.             <false/>  

  52.             <key>Type</key>  

  53.             <string>PSToggleSwitchSpecifier</string>  

  54.             <key>Title</key>  

  55.             <string>婚姻状况</string>  

  56.             <key>Key</key>  

  57.             <string>maritalStatus</string>  

  58.         </dict>  

  59.         <dict>  

  60.             <key>Type</key>  

  61.             <string>PSGroupSpecifier</string>  

  62.             <key>Title</key>  

  63.             <string>等级</string>  

  64.             <key>Key</key>  

  65.             <string></string>  

  66.         </dict>  

  67.         <dict>  

  68.             <key>DefaultValue</key>  

  69.             <integer>5</integer>  

  70.             <key>MaximumValue</key>  

  71.             <integer>10</integer>  

  72.             <key>MinimumValue</key>  

  73.             <integer>1</integer>  

  74.             <key>Type</key>  

  75.             <string>PSSliderSpecifier</string>  

  76.             <key>Title</key>  

  77.             <string>等级</string>  

  78.             <key>Key</key>  

  79.             <string>levelState</string>  

  80.         </dict>  

  81.     </array>  

  82.     <key>StringsTable</key>  

  83.     <string>Root</string>  

  84. </dict>  

  85. </plist>  

这时候运行,在来到设置项看:

已经是我们自己设置的效果了。

4、在程序中获取Settings 和写入Settings 添加UI

这里的项目是设置好了,那怎么读取呢?我们先在程序里添加一些对应的UI.打开.xib文件,往里放置控件,并生成对应的映射和Action。

 

pickerView的使用请参考iOS学习之UIPickerView控件的简单使用这篇文章。

5、实现读取设置和保存代码

关键是通过: NSUserDefaults *defaults = [NSUserDefaultsstandardUserDefaults];

代码获取设置项的NSUserDefaults值,然后通过key获取设置的内容和保存设置内容

在两个Button的按下事件实现如下:

[cpp] view plaincopy

  1. - (IBAction)getSettings:(id)sender {  

  2.     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];  

  3.     username.text =  [defaults objectForKey:@"username"];  

  4.     selectedAihao = [defaults objectForKey:@"aihao"];  

  5.     NSLog(@"aihao:%@",selectedAihao);  

  6.     NSInteger aihaoIndex = [aihaoValues indexOfObject:selectedAihao];  

  7.     [pickerView selectRow:aihaoIndex inComponent:0 animated:YES];  

  8.     [level setValue:[defaults integerForKey:@"levelState"]];  

  9. }  

  10.   

  11. - (IBAction)setSettings:(id)sender {  

  12.     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];  

  13.     [defaults setValue:username.text forKey:@"username"];  

  14.     NSInteger aihaoIndex = [aihaoTitles indexOfObject:selectedAihao];  

  15.   

  16.     [defaults setValue:[aihaoValues  objectAtIndex:aihaoIndex] forKey:@"aihao"];  

  17.     [defaults setInteger:level.value forKey:@"levelState"];  

  18.     UIAlertView *alert = [[UIAlertView alloc]   

  19.                           initWithTitle:@"偏好设置"  

  20.                           message:@"偏好设置已经保存!"  

  21.                           delegate:nil   

  22.                           cancelButtonTitle: @"完成"   

  23.                           otherButtonTitles:nil];  

  24.     [alert show];   

  25. }  


头文件实现:

[cpp] view plaincopy

  1. #import <UIKit/UIKit.h>  

  2.   

  3. @interface ViewController : UIViewController<UIPickerViewDelegate, UIPickerViewDataSource>  

  4. {  

  5.     NSMutableArray *aihaoTitles;  

  6.     NSMutableArray *aihaoValues;  

  7.     NSString *selectedAihao;  

  8. }  

  9.   

  10. @property (strong, nonatomic) IBOutlet UITextField *username;  

  11. @property (strong, nonatomic) IBOutlet UIPickerView *pickerView;  

  12. @property (strong, nonatomic) IBOutlet UISlider *level;  

  13. - (IBAction)getSettings:(id)sender;  

  14. - (IBAction)setSettings:(id)sender;  

  15. - (IBAction)doneEdit:(id)sender;  

  16.   

  17.   

  18. @end  


.m文件中其他代码:

[cpp] view plaincopy

  1. #import "ViewController.h"  

  2.   

  3. @interface ViewController ()  

  4.   

  5. @end  

  6.   

  7. @implementation ViewController  

  8. @synthesize username;  

  9. @synthesize pickerView;  

  10. @synthesize level;  

  11.   

  12. - (void)viewDidLoad  

  13. {  

  14.     [super viewDidLoad];  

  15.     aihaoTitles = [[NSMutableArray alloc] init];  

  16.     [aihaoTitles addObject:@"足球"];  

  17.     [aihaoTitles addObject:@"篮球"];  

  18.     [aihaoTitles addObject:@"乒乓球"];  

  19.     aihaoValues = [[NSMutableArray alloc] init];  

  20.     [aihaoValues addObject:@"football"];  

  21.     [aihaoValues addObject:@"basketball"];  

  22.     [aihaoValues addObject:@"pingpong"];  

  23.     // Do any additional setup after loading the view, typically from a nib.  

  24. }  

  25.   

  26. - (void)viewDidUnload  

  27. {  

  28.     [self setUsername:nil];  

  29.     [self setPickerView:nil];  

  30.     [self setLevel:nil];  

  31.     [super viewDidUnload];  

  32.     // Release any retained subviews of the main view.  

  33. }  

  34.   

  35. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  

  36. {  

  37.     return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);  

  38. }  

  39.   

  40. -(NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView  

  41. {  

  42.     return 1;  

  43. }  

  44.   

  45. -(NSInteger) pickerView:(UIPickerView *)pickerView   

  46. numberOfRowsInComponent:(NSInteger)component  

  47. {  

  48.     return [aihaoTitles count];  

  49. }  

  50.   

  51. -(NSString *) pickerView:(UIPickerView *)pickerView   

  52.              titleForRow:(NSInteger)row   

  53.             forComponent:(NSInteger)component  

  54. {  

  55.     return [aihaoTitles objectAtIndex:row];  

  56. }  

  57.   

  58. -(void) pickerView:(UIPickerView *)pickerView   

  59.       didSelectRow:(NSInteger)row   

  60.        inComponent:(NSInteger)component  

  61. {  

  62.     selectedAihao = [aihaoTitles objectAtIndex:row];  

  63. }  

  64. - (IBAction)doneEdit:(id)sender{  

  65.       

  66. }  


运行,输入姓名zhongguo 和爱好 足球,选择等级,保存设置。打开设置查看,可以读取到保存后的设置。

这样就可以操作和这只程序的设置项了。

例子代码:http://download.csdn.net/detail/totogo2010/4398462

著作权声明:本文由http://blog.csdn.net/totogo2010/原创,欢迎转载分享。请尊重作者劳动,转载时保留该声明和作者博客链接,谢谢

原文地址:https://www.cnblogs.com/guangleijia/p/5016557.html