<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                引言:以手機為例, 當你在用一款軟件聽音樂時,會發現手機自帶的大小聲控制鍵和播放軟件自帶大小聲控制鍵都可對聲音進行大小聲控制,而且他們的動作都會保持一致。那就讓我們一探究竟吧! 一:設置束(settings bundle),設置束是構建到應用程序中的一組plist文件,是他向系統設置(Setting:系統設置(Setting)圖標是在設備上默認有一個圖標,位于屏幕上)的應用程序發送消息 ,Setting應用程序會自動創建用戶界面。在ios應用程序當中,用戶默認設置是由NSUserDefault類來實現,NSUserDefault類可以看做是系統Setting與應用程序用戶Setting間溝通的橋梁,NSUserDefault類也是用鍵值來存取數據的,其特殊的一點就是NSUserDefault是將數據持久化的保存到文件系統中,而不是保存到內存的對象實例中。 二: ### 1) 創建Setting:Setting.bundle展開后需加入的plist文件模版,點擊http://download.csdn.net/detail/dongstone/4229357 ![](https://box.kancloud.cn/2016-06-12_575cce8075657.gif) ### 2)創建后運行結果: ![](https://box.kancloud.cn/2016-06-12_575cce80b0683.png) ### 三:具體的程序分析 ### 1)首先創建一個應用程序: ![](https://box.kancloud.cn/2016-06-12_575cce80e2d55.png) ### 2)向主屏幕(程序一開始最先顯示的視圖) ?拖控件: ![](https://box.kancloud.cn/2016-06-12_575cce811ec86.png) ### 3)向副視圖(這里的作用是與系統Setting一樣后臺控制主屏幕) ?添加控件: ![](https://box.kancloud.cn/2016-06-12_575cce815798e.png) ### 四: ?首先看到的是xcode模板為我們生成了兩個視圖控制器MainViewController和FlipsideViewController。 在這里MainViewController的作用相當與音樂播放器的前臺,而FlipsideViewController與系統Setting的作用是一樣的,對前臺數據進行操作。當然這樣舉例也不是很嚴謹,因為有的播放器程序前臺視圖界里面完全可以執行例如控制聲音大小的簡單操作,或者彈窗控制,做了更多優化。在這里為了化繁為簡,突出它的具體特性,講解一個小程序。 1)MainViewController.h ~~~ #import "FlipsideViewController.h" @interface MainViewController : UIViewController <FlipsideViewControllerDelegate> { UILabel *usernamelable; UILabel *passwordlable; UILabel *warplable; UILabel *warpfactorlable; } - (IBAction)showInfo:(id)sender; @property (nonatomic, strong) IBOutlet UILabel *usernamelable; @property (nonatomic, strong) IBOutlet UILabel *passwordlable; @property (nonatomic, strong) IBOutlet UILabel *warpDrivelable; @property (nonatomic, strong) IBOutlet UILabel *warpfactorlable; //自定義方法用于刷新數據 - (void)refreshFields; //自定義方法用于后臺返回響應 -(void)backupdata; @end ~~~ MainViewController.m ~~~ #import "MainViewController.h" @implementation MainViewController @synthesize usernamelable; @synthesize passwordlable; @synthesize warpDrivelable; @synthesize warpfactorlable; - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Release any cached data, images, etc that aren't in use. } #pragma mark obtain source from plist //刷新方法: -(void)refreshFields{ //與系統設置(Setting.bundle)相連,可相互傳遞數據 NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults]; //從沙盒里取值,這里的key是plist文件里定義好的 usernamelable.text=[defaults objectForKey:@"name"]; passwordlable.text=[defaults objectForKey:@"password"]; warpDrivelable.text=[defaults boolForKey:@"wrap"]?@"Enable":@"Disable"; warpfactorlable.text=[[defaults objectForKey:@"slider"] stringValue] ; NSLog(@"%@",[[defaults objectForKey:@"slider"] stringValue]); } #pragma mark - View lifecycle //視圖每次切換呈現時都會調用這個方法,這里需要注意的就是當按下iphone的home鍵時程序進入后臺,程序再走到前臺時并不會調用這個方法。 -(void)viewDidAppear:(BOOL)animated{ //刷新 [self refreshFields]; [super viewDidAppear:animated]; } - (void)viewDidLoad { //得到應用程序的單例對象 UIApplication *app=[UIApplication sharedApplication]; //將當前類作為觀察著,來接收應用程序的通知(從后臺走到前臺)來響應backupdata方法 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(backupdata) name:UIApplicationWillEnterForegroundNotification object:app]; [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } -(void)backupdata{ NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults]; //同步系統,當從手機自帶系統設置更改,程序從后臺回到前臺時將系統(磁盤)的setting數據同步到應用程序的Setting。 [defaults synchronize]; [self refreshFields]; } - (void)viewDidUnload { [self setUsernamelable:nil]; [self setPasswordlable:nil]; [self setWarpDrivelable:nil]; [self setWarpfactorlable:nil]; [super viewDidUnload]; // Release any retained subviews of the main view. // e.g. self.myOutlet = nil; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; } - (void)viewDidDisappear:(BOOL)animated { [super viewDidDisappear:animated]; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); } #pragma mark - Flipside View - (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller { [self dismissModalViewControllerAnimated:YES]; } - (IBAction)showInfo:(id)sender { FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:@"FlipsideViewController" bundle:nil]; controller.delegate = self; //設置view的過渡模式 controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; //呈現模式(效果就是切換視圖)。 [self presentModalViewController:controller animated:YES]; } @end ~~~ 2)FlipsideViewController.h ~~~ #import <UIKit/UIKit.h> @class FlipsideViewController; @protocol FlipsideViewControllerDelegate - (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller; @end @interface FlipsideViewController : UIViewController { UISwitch *_MySwitch; UISlider *_MySilder; } @property (weak, nonatomic) IBOutlet id <FlipsideViewControllerDelegate> delegate; @property (nonatomic, strong) IBOutlet UISwitch *MySwitch; @property (nonatomic, strong) IBOutlet UISlider *MySilder; - (IBAction)done:(id)sender; //自定義方法視圖從后臺返回時將被調用。 -(void)applicationWillEnterForeground; //用于改變系統數據 - (IBAction)myswitchchange:(id)sender; - (IBAction)mysliderchange:(id)sender; //刷新數據 -(void)refreshField; @end ~~~ FlipsideViewController.m ~~~ #import "FlipsideViewController.h" @implementation FlipsideViewController @synthesize delegate = _delegate; @synthesize MySwitch = _MySwitch; @synthesize MySilder = _MySilder; - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Release any cached data, images, etc that aren't in use. } #pragma mark - View lifecycle - (void)viewDidLoad { //得到應用程序對象用于做通知的過濾條件 UIApplication *app=[UIApplication sharedApplication]; //添加觀察者為self,接收應用程序的通知,響應方法為applicationWillEnterForeground。(也就是程序從后臺進入前臺) [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillEnterForeground) name:UIApplicationWillEnterForegroundNotification object:app]; //刷新數據 [self refreshField]; [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)viewDidUnload { [self setMySwitch:nil]; [self setMySilder:nil]; [super viewDidUnload]; // Release any retained subviews of the main view. // e.g. self.myOutlet = nil; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; } - (void)viewDidDisappear:(BOOL)animated { [super viewDidDisappear:animated]; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); } -(void)applicationWillEnterForeground{ NSUserDefaults *defau=[[NSUserDefaults alloc]init]; //同步系統,刷新系統(磁盤)的Setting [defau synchronize]; [self refreshField]; } - (IBAction)myswitchchange:(id)sender { NSUserDefaults *defau=[[NSUserDefaults alloc] init]; [defau setBool:self.MySwitch.on forKey:@"wrap"]; //同步系統,刷新系統(磁盤)的setting [defau synchronize]; } - (IBAction)mysliderchange:(id)sender { NSUserDefaults *defau=[[NSUserDefaults alloc] init]; [defau setFloat:self.MySilder.value forKey:@"slider"]; //同步系統,刷新系統(磁盤)的setting [defau synchronize]; } -(void)refreshField{ NSUserDefaults *defua=[[NSUserDefaults alloc] init]; //同步系統,刷新系統(磁盤)的setting [defua synchronize]; self.MySwitch.on=[defua boolForKey:@"wrap"]; self.MySilder.value=[defua floatForKey:@"slider"]; } #pragma mark - Actions - (IBAction)done:(id)sender { [self.delegate flipsideViewControllerDidFinish:self]; } @end ~~~ ### 3)小結:這個小程序里也包含了很多別的知識如:系統自動生成的部分,頁面間的反轉切換方法的調用,信息傳遞等。以前寫過,有詳細講解(點擊參見:[(7)---02 iphone 開發 數據傳遞 : 頁面切換與數據的反向傳遞以及協議(protocol)作用的體現](http://blog.csdn.net/dongstone/article/details/7457320))。 ### 程序結果: ![](https://box.kancloud.cn/2016-06-12_575cce8198ccc.gif)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看