IOS-生命周期-Swift

App生命周期

应用状态

App主要有五种状态,分别是:

未运行——Not running

应用程序没启动

未激活——Inactive

程序在前台运行,不过没有接收到事件。
一般每当应用要从一个状态切换到另一个不同的状态时,中途过渡会短暂停留在此状态。唯一在此状态停留时间比较长的情况是:当用户锁屏时,或者系统提示用户去响应某些(诸如电话来电、有未读短信等)事件的时候。

激活——Active

程序在前台运行而且接收到了事件,这也是前台的一个正常的模式。

后台——Backgroud

程序在后台而且能执行代码,大多数程序进入这个状态后会在在这个状态上停留一会,时间到之后会进入挂起状态(Suspended),有的程序经过特殊的请求后可以长期处于Backgroud状态。

挂起——Suspended

程序在后台不能执行代码。系统会自动把程序变成这个状态而且不会发出通知。当挂起时,程序还是停留在内存中的,当系统内存低时,系统就把挂起的程序清除掉,为前台程序提供更多的内存。

关系图

在这里插入图片描述

生命周期方法

APP的生命周期就是UIApplicationDelegate中的回调方法,这些方法是根据状态变化进行响应的地方,其中最常用的就是以下7个方法:

application:willFinishLaunchingWithOptions:App启动时调用表示应用加载进程已经开始,常用来处理应用状态的存储和恢复

application:didFinishLaunchingWithOptions:
表示App将从未运行状态进入运行状态,用于对App的初始化操作

applicationDidBecomeActive:
当应用即将进入前台运行时调用

applicationWillResignActive:
当应用即将进从前台退出时调用

applicationDidEnterBackground:
当应用开始在后台运行的时候调用

applicationWillEnterForeground:
当程序从后台将要重新回到前台(但是还没变成Active状态)时候调用

applicationWillTerminate:
当前应用即将被终止,在终止前调用的函数。通常是用来保存数据和一些退出前的清理工作。如果应用当前处在suspended,此方法不会被调用。 该方法最长运行时限为5秒,过期应用即被kill掉并且移除内存。

相关方法

AppDelegate实现了UIApplicationDelegate,有以下方法可供测试:

	//在App启动时调用表示应用加载进程已经开始,常用来处理应用状态的存储和恢复
    func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        print("willFinishLaunchingWithOptions")
        print("——应用开始加载——")
        return true
    }
    //表示App将从未运行状态进入运行状态,用于对App的初始化操作
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        print("调用:didFinishLaunchingWithOptions")
        print("——应用开始运行——")
        return true
    }
    //当应用即将进入前台运行时调用
    func applicationDidBecomeActive(_ application: UIApplication) {
        print("调用:applicationDidBecomeActive")
        print("——应用即将进入前台——")
    }
    //当应用即将进从前台退出时调用
    func applicationWillResignActive(_ application: UIApplication) {
        print("调用:applicationWillResignActive")
        print("——应用即将从前台退出——")
    }
    //当程序从后台将要重新回到前台(但是还没变成Active状态)时候调用
    func applicationWillEnterForeground(_ application: UIApplication) {
        print("调用:applicationWillEnterForeground")
        print("——应用即将从后台进入前台——")
    }
    //当应用开始在后台运行的时候调用
    func applicationDidEnterBackground(_ application: UIApplication) {
        print("调用:applicationDidEnterBackground")
        print("——应用开始在后台运行——")
    }
    //当前应用即将被终止,在终止前调用的函数。通常是用来保存数据和一些退出前的清理工作。如果应用当前处在suspended,此方法不会被调用。 该方法最长运行时限为5秒,过期应用即被kill掉并且移除内存
    func applicationWillTerminate(_ application: UIApplication) {
        print("调用:applicationWillTerminate")
        print("——应用即将终止——")
    }

注意

在这里插入图片描述

虽然都有这些方法,但是我实测下来,在AppDelegate种有的方法并没有打印信息,也就说明没有被调用。

applicationDidBecomeActive

applicationWillResignActive

applicationWillEnterForeground

applicationDidEnterBackground

这几个方法并没有打印信息,不管我如何在前台或者后台。因此,我又在SceneDelegate中找到了对应的方法:

func sceneDidBecomeActive(_ scene: UIScene) {
        // Called when the scene has moved from an inactive state to an active state.
        // Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
        print("调用:applicationDidBecomeActive")
        print("——应用即将进入前台——")
    }

    func sceneWillResignActive(_ scene: UIScene) {
        // Called when the scene will move from an active state to an inactive state.
        // This may occur due to temporary interruptions (ex. an incoming phone call).
        print("调用:applicationWillResignActive")
        print("——应用即将从前台退出——")
    }

    func sceneWillEnterForeground(_ scene: UIScene) {
        // Called as the scene transitions from the background to the foreground.
        // Use this method to undo the changes made on entering the background.
        print("调用:applicationWillEnterForeground")
        print("——应用即将从后台进入前台——")
    }

    func sceneDidEnterBackground(_ scene: UIScene) {
        // Called as the scene transitions from the foreground to the background.
        // Use this method to save data, release shared resources, and store enough scene-specific state information
        // to restore the scene back to its current state.
        print("调用:applicationDidEnterBackground")
        print("——应用开始在后台运行——")
    }

在这里插入图片描述
这次就完整的打印了生命周期方法,操作依次是:运行、按一次Home键返回桌面、重新进入App、双击Home键然后Kill掉应用。

在其他地方监听

除了在AppDelegate和SceneDelegate中可以监听生命周期之外,还可以在ViewController等地方监听,代码如下:

	func applicationNotification() {
        //应用开始在后台运行的时候通知
        NotificationCenter.default.addObserver(self, selector: #selector(applicationDidEnterBackground), name: UIApplication.didEnterBackgroundNotification, object: nil)
        //当程序从后台将要重新回到前台(但是还没变成Active状态)时候通知
        NotificationCenter.default.addObserver(self, selector: #selector(applicationWillEnterForeground), name: UIApplication.willEnterForegroundNotification, object: nil)
        //应用进入运行状态通知
        NotificationCenter.default.addObserver(self, selector: #selector(applicationDidFinishLaunching), name: UIApplication.didFinishLaunchingNotification, object: nil)
        //应用即将进入前台运行时通知
        NotificationCenter.default.addObserver(self, selector: #selector(applicationDidBecomeActive), name: UIApplication.didBecomeActiveNotification, object: nil)
        //应用即将进从前台退出时通知
        NotificationCenter.default.addObserver(self, selector: #selector(applicationWillResignActive), name: UIApplication.willResignActiveNotification, object: nil)
        //应用因内存警告退出通知
        NotificationCenter.default.addObserver(self, selector: #selector(applicationDidReceiveMemoryWarning), name: UIApplication.didReceiveMemoryWarningNotification, object: nil)
        //双击Home键杀掉应用通知
        NotificationCenter.default.addObserver(self, selector: #selector(applicationWillTerminate), name: UIApplication.willTerminateNotification, object: nil)
    }
    @objc func applicationDidEnterBackground() {
        print("app status : applicationDidEnterBackground——APP开始在后台运行")
    }

    @objc func applicationWillEnterForeground() {
        print("app status : applicationWillEnterForeground——APP从后台重新进入前台")
    }

    @objc func applicationDidFinishLaunching() {
        print("app status : applicationDidFinishLaunching——APP进入运行状态")
    }
    @objc func applicationDidBecomeActive() {
        print("app status : applicationDidBecomeActive——APP即将进入前台")
    }

    @objc func applicationWillResignActive() {
        print("app status : applicationWillResignActive——APP即将从前台退出")
    }

    @objc func applicationDidReceiveMemoryWarning() {
        print("app status : applicationDidReceiveMemoryWarning——APP因内存警告退出")
    }

    @objc func applicationWillTerminate() {
        print("app status : applicationWillTerminate——APP应用将被终止")
    }

结果如图:
在这里插入图片描述

ViewController生命周期

常用的控制器生命周期方法如下:

	override func viewDidLoad() {
        super.viewDidLoad()
        print("viewDidLoad-当视图控制器的视图加载完成后调用")
    }
    override func viewWillAppear(_ animated: Bool) {
        print("viewWillAppear-当视图即将显示在屏幕上时调用")
    }
    override func viewWillLayoutSubviews() {
        print("viewWillLayoutSubviews-将要对子视图进行调整")
    }
    override func viewDidLayoutSubviews() {
        print("viewDidLayoutSubviews-对子视图调整完毕")
    }
    override func viewDidAppear(_ animated: Bool) {
        print("viewDidAppear-当视图已经显示在屏幕上时调用")
    }
    override func viewWillDisappear(_ animated: Bool) {
        print("viewWillDisappear-当视图即将从屏幕上消失时调用")
    }
    override func viewDidDisappear(_ animated: Bool) {
        print("viewDidDisappear-当视图已经从屏幕上消失时调用")
    }
    override func didReceiveMemoryWarning() {
        print("didReceiveMemoryWarning-当系统内存不足时调用")
    }

结果如图:
在这里插入图片描述

UIView生命周期

UIView常用的生命周期方法如下:

import Foundation
import UIKit

class Button:UIButton{

    override func willRemoveSubview(_ subview: UIView) {
        super.willRemoveSubview(subview)
        print("willRemoveSubview-当子视图从本视图移除时调用")
    }

    override func willMove(toSuperview newSuperview: UIView?) {
        super.willMove(toSuperview: newSuperview)
        print("willMove-当视图即将加入父视图时 / 当视图即将从父视图移除时调用")
    }

    override func didMoveToSuperview() {
        super.didMoveToSuperview()
        print("didMoveToSuperview-当视图加入父视图时 / 当视图从父视图移除时调用")
    }

    override func willMove(toWindow newWindow: UIWindow?) {
        super.willMove(toWindow: newWindow)
        print("willMove-当视图即将加入window视图时 / 当视图即将从window视图移除时调用")
    }

    override func didMoveToWindow() {
        super.didMoveToWindow()
        print("didMoveToWindow-当视图加入window视图时 / 当视图从window视图移除时调用")
    }
    override func didAddSubview(_ subview: UIView) {
        super.didAddSubview(subview)
        print("didAddSubview-当视图添加子视图时调用")
    }
}

这里是通过创建一个自定义的按钮监听,然后使用:

    var button:Button!
    override func viewDidLoad() {
        super.viewDidLoad()
        
        button=Button(frame: CGRect(x: 100, y: 100, width: 100, height: 80))
        button.backgroundColor=UIColor.orange
        button.setTitle("按钮", for: .normal)
        self.view.addSubview(button)
        button.addTarget(self, action: #selector(click), for: UIControl.Event.touchUpInside)
    }
    @objc func click(){
        button.removeFromSuperview()
    }
    

点击按钮然后将其从父布局中移除,结果如下:
在这里插入图片描述
参考链接:iOS 生命周期 (最新最完整)

  • 46
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
对于一名本科应届生在 iOS 开发方面的面试,通常会涉及以下内容: 1. Swift 语言基础:面试官可能会问你关于 Swift 语言的基本语法、数据类型、集合类型、函数、闭包等方面的问题。确保你对 Swift 语言的基础知识有一定的了解。 2. iOS 开发基础:你需要掌握 iOS 开发的基本框架和概念,如 UIKit、AutoLayout、Core Data、Networking 等。了解 MVC 架构、生命周期、UI 组件、数据持久化等内容。 3. Xcode 工具使用:熟悉 Xcode 的使用和常用功能,如项目创建、调试、界面设计等。 4. UI/UX 设计:了解基本的 UI/UX 设计原则和设计模式,能够根据设计稿实现界面布局和样式。 5. 网络通信:了解网络请求的原理和常用库,如 Alamofire、NSURLSession 等。了解 RESTful API 的基本概念和使用。 6. 数据库和数据持久化:熟悉 Core Data 或其他常用的数据库框架,能够进行数据的增删改查操作。 7. 版本控制工具:熟悉 Git 的基本使用,包括代码提交、分支管理、冲突解决等。 8. 项目经验:准备好介绍你在学习过程中完成的项目,包括项目目标、技术难点、解决方案等。展示你的实际开发经验和能力。 除了上述内容,还有可能会问到算法和数据结构、性能优化、多线程编程等相关知识。在面试前,建议你复习以上内容,并准备一些常见面试题的回答,以便在面试中更好地展示你的技能和潜力。祝你面试顺利!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值