开发过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  。

欢迎大家訪问!

iOS界面生命周期过程具体解释的更多相关文章

  1. 简单介绍关于IOS的生命周期过程

    初步了解一下生命周期的过程: 1.通过alloc init 分配内存,初始化controller. 2.loadViewloadView方法默认实现[super loadView]如果在初始化cont ...

  2. (转)深入浅出 iOS 之生命周期

    原文:http://www.cocoachina.com/applenews/devnews/2011/0817/3129.html 深入浅出 iOS 之生命周期 发布于:2011-08-17 10: ...

  3. 【Xamarin 开发 IOS --IOS ViewController生命周期】

    ViewController ViewController是IOS开发中MVC模式中的C,ViewController是view的controller,ViewController的职责主要包括管理内 ...

  4. 【转】深入浅出 iOS 之生命周期

    [iOS]深入浅出 iOS 之生命周期 深入浅出 iOS 之生命周期  http://blog.csdn.net/kesalin/article/details/6691766 罗朝辉(http:// ...

  5. 图解ios程序生命周期

    图解ios程序生命周期 应用程序启动后状态有Active.Inactive.Background.Suspended.Not running这5种状态,几种状态的转换见下图: 在AppDelegate ...

  6. iOS的生命周期

    iOS应用程序一般都是由自己编写的代码和系统框架组成.系统框架提供了一些基本的infrastructure给APP来运行,而开发者则自己编写代码定制APP的外观和行为,了解iOS infrastruc ...

  7. iOS视图生命周期

    视图是应用的一个重要组成部分,功能的实现与其息息相关,而视图控制器控制着视图,其重要性在整个应用中不言而喻. 1.视图生命周期与视图控制器关系 以视图的4 种状态为基础,我们来系统了解一下视图控制器的 ...

  8. iOS ViewController生命周期

    ViewController是view的controller,viewController的职责主要包括管理内部各个view的加载显示与卸载,同时负责与其他ViewController的通信和协调. ...

  9. 深入浅出 iOS 之生命周期

    转:http://blog.csdn.net/kesalin/article/details/6691766 iOS应用程序的生命周期相比 Android 应用程序的生命周期来说,没那么简明易懂,但是 ...

随机推荐

  1. Vuejs2.0构建一个彩票查询WebAPP(2)

    一,Vuex的使用 import Vue from 'vue' import Vuex from 'vuex' import MsgModules from './MsgModules' Vue.us ...

  2. javaScript call与apply学习笔记

    call和apply是借用他人的函数实现自己到功能,具体表现在改变this指向,借用他人方法 而不同的地方是call是把实参按照形参的个数传入,而apply传入的是一个数组(argument) 写一个 ...

  3. linux下,yum 安装mysql

    顺手记录一下安装mysqlclient 先安装mysql-devel yum install mysql-devel 再安装mysqlclient pip3 install mysqlclient 开 ...

  4. oracle查询字段大于指定长度的数据

    select * from MES_MACHINE_RECORD t where length(t.bar_code2)<10 ;

  5. 【Codeforces Round #424 (Div. 2) C】Jury Marks

    [Link]:http://codeforces.com/contest/831/problem/C [Description] 有一个人参加一个比赛; 他一开始有一个初始分数x; 有k个评委要依次对 ...

  6. 【2017 Multi-University Training Contest - Team 1 1011】KazaQ's Socks

    [Link]:http://acm.hdu.edu.cn/showproblem.php?pid=6043 [Description] 一个人壁橱里有n双袜子,每天早上取一双最小下标的袜子,然后晚上放 ...

  7. AVEVA PDMS to 3ds Max - RvmTranslator6.0beta

    AVEVA PDMS to 3ds Max - RvmTranslator6.0beta eryar@163.com RvmTranslato6.0 translate PDMS RVM to 3ds ...

  8. 路由及路由器工作原理深入解析3:路由与port

        日志"路由及路由器工作原理深入解析1"http://user.qzone.qq.com/2756567163/blog/1438322342介绍了"为什么要使用路 ...

  9. ip6tables: ipv6-icmp vs icmp

    ip6tables: ipv6-icmp vs icmp资料来源 https://www.jethrocarr.com/2013/02/09/ip6tables-ipv6-icmp-vs-icmp/I ...

  10. js关于循环的理解

    学习任何语言都离不开循环,js也是一样,看了网上的资料,整理一份关于js循环的理解. 1.最基础循环,js和其他高级语言一样使用for.while循环 (function() { for(var i= ...