• App状态管理-AppDelegate


    前面提到AppDelegate的实例在UIApplicationMain函数中被创建并调用。

    AppDelegate的主要作用,可以从其继承类可以看出 class AppDelegate:UIResponder,UIApplicationDelegate

    1. UIResponder

    UIApplication,UIView,UIViewController,UIWindow等需要处理event的类都需要继承这个类。
    该类可以处理iOS的所有程序员需要处理的动作event,包括touch events和motion events。
    在AppDelegate类中一般不需要对该类的继承的东西进行处理,具体处理放到实际的view,viewcontroller中去做。

    2. UIApplicationDelegate

    这个是AppDelegate类中重点需要override的变量。官方手册 UIApplicationDelegate
    UIApplicationDelegate中负责管理app的各种状态,官方手册有详细的介绍。这里只记录一些最典型的例子。
    关于app的各种状态参见下图,每个状态变化时都可以override父类的方法进行编程。



    代理中所有函数的第一个参数为UIApplication的单例对象,通过这个参数可以获得app的信息,比如Window的引用等。

    1.当点击app即将启动时,app状态由Not Running切换到Inactive,此时如果想进行编程,可以override函数:
    application(_:willFinishLaunchingWithOptions:)


    2.当app完成初始处理后,即将显示画面前,会执行函数
    
application(_:didFinishLaunchingWithOptions:)


    3.当函数被active后,如果想在看到画面前执行一些动作
    


applicationDidBecomeActive(_:)


    4.当app即将被切换到后台
    


applicationWillResignActive(_:)


    5.当app已经进入后台(注意进入后台的程序会因为系统内存等原因,可能随时被suspended,即被系统关闭)
    


applicationDidEnterBackground(_:)


    6.当app从后台进入前台
    


applicationWillEnterForeground(_:)


    7.这个函数目前基本上用不到了,即使是被手动退出app(双击home,进入退出)也不会被执行。
    如果想在程序退出时执行该函数,需要在Xcode中info.plist设置 Application does not run in background
    


applicationWillTerminate(_:)

    在各method中打印出信息,可以测试其运行
    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {
                                
        var window: UIWindow?
    
    
        func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
            println("didFinishLaunchingWithOptions")
            return true
        }
    
        func applicationWillResignActive(application: UIApplication!) {
            println("applicationWillResignActive")
        }
    
        func applicationDidEnterBackground(application: UIApplication!) {
            println("applicationDidEnterBackground")
        }
    
        func applicationWillEnterForeground(application: UIApplication!) {
            println("applicationWillEnterForeground")
        }
    
        func applicationDidBecomeActive(application: UIApplication!) {
            println("applicationDidBecomeActive")
        }
    
        func applicationWillTerminate(application: UIApplication!) {
            println("applicationWillTerminate")
        }
    }
  • 相关阅读:
    2019.9.21 Tomcat基于端口的虚拟主机
    shell脚本作业
    DNS原理及其解析过程
    用户管理系统脚本
    pxe批量装机
    磁盘分区挂载脚本
    安装apache脚本
    linux远程拷贝命令及not a regular file 解决方案
    卸载虚拟网卡的方法
    watch的用法
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/4875268.html
Copyright © 2020-2023  润新知