• 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


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


    github主页:https://github.com/chenyufeng1991  。

    欢迎大家訪问!

  • 相关阅读:
    P4097 [HEOI2013]Segment(李超线段树模板)
    P2155 [SDOI2008]沙拉公主的困惑
    BZOJ3675 [Apio2014]序列分割[斜率优化dp]
    hdu4261 Estimation[暴力dp+对顶堆]
    poj2374 Fence Obstacle Course[线段树+DP]
    poj1463 Strategic game[树形DP]
    CH5E02 [IOI1999]花店橱窗[暴力dp]
    CH5E01[NOIP2010] 乌龟棋[暴力]
    CH5702 Count The Repetitions[倍增dp]
    P1081 [NOIP2012]开车旅行[倍增]
  • 原文地址:https://www.cnblogs.com/mfmdaoyou/p/6993513.html
Copyright © 2020-2023  润新知