iOS开发:第一个iOS程序分析——代理,生命周期函数

上一篇文章中我们开发了第一个iOS应用程序,用来计算两个整数的和,今天我们将介绍其中的两个文件:

1.main.m文件

首先展开supporting Files并且打开其下的main.m文件,其中main函数就是应用程序的入口,main函数只是标记了一个自动释放池自动决定何时释放内存,并且返回了一个函数UIApplicationMain的执行结果。

换句话说,main函数直接调用UIApplicationMain执行,该UIApplicationMain接受的比较重要的最后一个参数为一个代理类的名字,UIApplicationMain据此参数来创建这个AppDelegate代理类,由AppDelegate通过对Application的控制来控制程序的生命周期。

那么什么是代理(又叫委托模式):

苹果官方文档对代理模式的解释:

“Delegation is a simple and powerful pattern in which one object in a program acts on behalf of, or in coordination with, another object. The delegating object keeps a reference to the other object—the delegate—and at the appropriate time sends a message to it. The message informs the delegate of an event that the delegating object is about to handle or has just handled. The delegate may respond to the message by updating the appearance or state of itself or other objects in the application, and in some cases it can return a value that affects how an impending event is handled. The main value of delegation is that it allows you to easily customize the behavior of several objects in one central object.”

"代理是一种简单而强大的设计模式,在这种模式下,程序中的一个对象与另一个对象进行协调或响应。代理类持有委托方的一个引用并且在适当的时候向委托方发送一条消息,该消息通知委托方将有一个事件会被代理类处理或者代理类刚刚处理了一个事件。委托方可能会通过更新自身或其他对象的界面或状态来响应这条消息。在某些情况下他也能返回一个值来影响下一个即将被处理的事件。代理模式最主要的作用就是能够允许你对围绕同一个对象的多个对象进行更容易的自定义行为。"




接下来看看AppDelegate是通过哪些方法来控制Application的生命周期的:

2.AppDelegate.m文件

打开KZXAppDelegate.m文件,有如下方法

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

- (void)applicationWillResignActive:(UIApplication *)application

- (void)applicationDidEnterBackground:(UIApplication *)application

- (void)applicationWillEnterForeground:(UIApplication *)application

- (void)applicationDidBecomeActive:(UIApplication *)application

- (void)applicationWillTerminate:(UIApplication *)application


这些方法都是以上介绍的——Application委托给AppDelegate处理的事件——都交给

AppDelegate管理。

didFinishLaunchingWithOptions根据函数名可以看出,

程序运行后最先发生的是didFinishLaunchingWithOptions

事件,Application通知AppDelegate处理该事件,

因此程序的初始化应该在这里进行。

applicationWillResignActive:当程序将要失去焦点(变成不可见),

不能与用户交互的时候调用,

该方法通常用来保存一下界面状态。

applicationDidEnterBackground:当程序已经进入后台时调用,

此时应用程序完全不可见,

通常可以释放一些进入后台后不用的内存。

applicationWillEnterForeground:当程序即将进入前台,

恢复可见并且可与用户交互

的时候调用,通常用来恢复在applicationWillResignActive

方法保存的状态。

applicationDidBecomeActive:当程序回复激活状态时调用,

此时界面可见,

用户可以与程序交互,通常用来恢复程序状态。

applicationWillTerminate:当程序结束时调用,

SDK4.0以前当用户在当前程序运行时

按下Home键时调用,SDK4.0以上支持多任务,

默认不会被执行,可以在Plist中设置

Application does not run in backgroud 为YES。

通常用来保存数据,释放所有资源。


总结一下:

1.和其他很多编程语言一样(swift除外),Objective-C也是从main函数开始,

main函数使用代理类来管理程序的生命周期。

2.代理类KZXAppDelegate通过实现AppDelegate协议实现相应的方法来处理

委托方Application的事件,从而控制App的生命周期。


下次我们将讨论AppDelegate.h文件和视图View。


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值