iOS開發之iOS程序偏好設置(Settings Bundle)的使用

在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

  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>  

這時候運行,在來到設置項看:


已是咱們本身設置的效果了。

四、在程序中獲取Settings 和寫入Settings 添加UI

這裏的項目是設置好了,那怎麼讀取呢?咱們先在程序裏添加一些對應的UI.打開.xib文件,往裏放置控件,並生成對應的映射和Action。

 

pickerView的使用請參考iOS學習之UIPickerView控件的簡單使用這篇文章。

五、實現讀取設置和保存代碼

關鍵是經過: 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/原創,歡迎轉載分享。請尊重做者勞動,轉載時保留該聲明和做者博客連接,謝謝

謝謝這篇文章的做者,講解的很詳細,頗有用

相關文章
相關標籤/搜索