IOS 控制項的生命週期

來源:互聯網
上載者:User

標籤:os   art   cti   for   io   代碼   

ViewController的生命週期包括: 
Initialize 
ViewDidLoad 
ViewWillAppear 
ViewDidAppear 
ViewWillDisappear 
ViewDidDisappear 
ViewDidUnload 
Dispose 

      對於Application來說,ViewController的每個階段都對應了一個方法,IOS會在適當的時間調用對應的方法,因此,我們可以在每個方法中添加對應的代碼來做我們想做的事。需要注意的是,這些方法都是為ViewController服務的,對於Views對象來說,是沒有這些方法的。 
       View的生命週期方法如下: 

   * ViewDidLoad - Called when you create the class and load from xib. Great for initial setup and one-time-only work 

   * ViewWillAppear - Called right before your view appears, good for hiding/showing fields or any operations that you want to happen every time before the view is visible. Because you might be going back and forth between views, this will be called every time your view is about to appear on the screen 

   * ViewDidAppear - Called after the view appears - great place to start an animations or the loading of external data from an API. 

   * ViewWill/DidDisappear - Same idea as the WillAppear. 

   * ViewDidUnload/Dispose - Available to you,  In objective-c, this is where you do your cleanup and release of stuff, but this is handled automatically so not much you really need to do here. 

Application的生命週期包括: 
      在IOS中,生命週期就是由一系列的方法構成, 對於一個App來說,涉及到的生命週期有: 
1、點擊icon啟動,對應的方法有: 
      didFinishLaunchingWithOptions: 
      applicationDidBecomeActive: 
2、按下Home鍵,返回案頭,對應方法: 
      applicationWillResignActive 
      applicationDidBecomeInactive 
3、再點擊icon回到前台,對應方法: 
      applicationWillEnterForegroud 
      applicationDidBecomeActive 
4、Application忽然被終止,對應方法: 
     applicationWillTerminate 
     這兒需要注意,點2下home鍵,把背景程式關掉不會調用這個方法,在蘋果的IOS開發指南上有如下描述: 
”Even if you develop your application using iPhone SDK 4 and later, you must still be prepared for your application to be terminated. If memory becomes constrained, the system might remove applications from memory in order to make more room. If your application is currently suspended, the system removes your application from memory without any notice. However, if your application is currently running in the background, the system does call the applicationWillTerminate:method of the application delegate. Your application cannot request additional background execution time from this method.“ 就是說,在機器記憶體不足時,IOS會清理背景程式,在這個時候會調用該方法,一般情況下,很少會用到這個方法,盡量不要在這個方法裡寫你的應用邏輯。 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.