码迷,mamicode.com
首页 > 移动开发 > 详细

iOS界面生命周期过程详解

时间:2015-07-25 18:30:00      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:swift   xcode   ios   

       开发过Android的人都知道,每一个Android界面就是一个Activity,而每一个Activity都会有自己的生命周期, 有一系列方法会控制Activity的生命周期,如:onCreate(),onStart(),onResume(),onDestroy()等等。在iOS中,也会有这样的流程控制。这篇博客先来讨论一个iOS应用的控制流程。

       在新创建的一个程序中会有一个AppDelegate.swift文件,里面包含的一系列方法体现了iOS的控制流程。下面是系统自己生成的代码,我们来感受一下:

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        return true
    }

    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
        
        
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
        
        
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
        
       
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
        
       
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
        
        
    }


}
其中的注释也是代码自动生成的,下面我们详细介绍一下这几个方法。

(1)func applicationWillResignActive(application: UIApplication)
该方法在当前应用程序即将退出活动状态时会被调用,如界面即将退回到主界面时,会被调用。可以和Android中的onPause()方法比较。官方的解释中还说,程序的暂时停止运行,如进来一个电话,或者短信都会调用该方法。


(2)func applicationDidEnterBackground(application: UIApplication)

该方法一般在applicationWillResignActive()后调用,表示应用将会进入后台运行。官方的解释中还说,程序调用这个方法会释放共享的资源,并保存用户的数据,同时也会存储当前应用程序的信息,供程序恢复运行时使用。注意:如果你的应用程序支持后台运行,那么当用户退出程序时,将不会调用applicationWillTerminate()方法,而是调用applicationDidEnterBackground() 方法。


(3)func applicationWillEnterForeground(application: UIApplication)

该方法在应用程序进入活动状态(前台)时调用。如本来在后台运行,现在点击图标,重新运行程序活动。


(4)func applicationDidBecomeActive(application: UIApplication)
该方法一般在applicationWillEnterForeground()方法之前调用,表示应用程序即将进入前台。


(5)func applicationWillTerminate(application: UIApplication)

该方法在程序被终止时调用,如从最近运行程序中删除当前程序,就会调用该方法。


        下面我们对上述代码做一点修改,在每一个方法中输出一句话,并在模拟器中做各种操作,观察输出结果,就可以知道整个应用的执行流程。

func applicationWillResignActive(application: UIApplication) {
        
        println("Application Will Resign Active")
    }

    func applicationDidEnterBackground(application: UIApplication) {
        
        println("Application Did Enter Background")
    }

    func applicationWillEnterForeground(application: UIApplication) {
        
        
        println("Application Will Enter Foreground")
    }

    func applicationDidBecomeActive(application: UIApplication) {
        
        
        println("Application Did Become Active")
    }

    func applicationWillTerminate(application: UIApplication) {
        
        
        println("Application Will Terminate")
    }

(一)启动-->按Home键

   执行结果:Application Did Become Active-->Application Will Resign Active-->Application Did Enter background


(二)启动-->按Home键-->点击图标再次启动

执行结果:Application Did Become Active-->Application Will Resign Active-->Application Did Enter background-->Application Will Enter Foreground-->Application Did Become Active


(三)启动-->从最近应用程序中删除

Application Did Become Active-->Application Will Resign Active-->Application Did Enter background-->Application Will Terminate


      上述是比较简单的操作控制,以后还会有更为复杂的,我们到时候在慢慢研究。

版权声明:本文为博主原创文章,未经博主允许不得转载。

iOS界面生命周期过程详解

标签:swift   xcode   ios   

原文地址:http://blog.csdn.net/chenyufeng1991/article/details/47057563

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!