首页 > 代码库 > IOS 控件的生命周期

IOS 控件的生命周期

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会清理后台程序,在这个时候会调用该方法,一般情况下,很少会用到这个方法,尽量不要在这个方法里写你的应用逻辑。