Kenshin Cui's Blog 博客AppDelegate程序运行过程理解

来源:互联网 发布:日本电影知乎 编辑:程序博客网 时间:2024/04/30 12:28

main.m

int main(int argc,char * argv[]) {

    @autoreleasepool {

        //一般从main函数开始。但是在main函数中我们其实只能看到一个方法,这个方法内部是一个消息循环(相当于死循环),因此运行到这个方法UIApplicationMain之后程序不会自动退出,而只有用户手动关闭循环才会结束,其中四个参数。

        returnUIApplicationMain(argc,/*参数个数*/ argv/*参数内容*/,nil,NSStringFromClass([AppDelegateclass]));

        /*第三个参数代表UIApplication类(或子类)字符串,这个参数默认为nil则代表默认为UIApplication类,用户可以自定义一个类继承于这个类;如果为nil则等价于NSStringFromClass([UIApplication class]),大家可以自己试验,效果完全一样;UIApplication是单例模式,一个应用程序只有一个UIApplication对象或子对象;*/

        /*第四个参数是UIApplication的代理类字符串,默认生成的是AppDelegate类,这个类主要用于监听整个应用程序生命周期的各个事件(其实类似于之前我们文章中提到的事件监听代理),当UIApplication运行过程中引发了某个事件之后会调用代理中对应的方法;*/

        /*也就是说当执行UIApplicationMain方法后这个方法会根据第三个参数创建对应的UIApplication对象,这个对象会根据第四个参数AppDelegate创建并指定此对象为UIApplication的代理;同时UIApplication会开启一个消息循环不断监听应用程序的各个活动,当应用程序生命周期发生改变UIApplication就会调用代理对应的方法。*/

    }

}


AppDelegate.m


//  Created by dllo on 15/8/17.

//  Copyright (c) 2015赵洪伟. All rights reserved.

//


#import "AppDelegate.h"

//使用的是延展

@interface AppDelegate ()


@end


@implementation AppDelegate

//这个类中定义了应用程序生命周期中各个事件的执行方法

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

//程序启动之后执行,只有在第一次程序启动后才执行,以后不再执行

    returnYES;

}


- (void)applicationWillResignActive:(UIApplication *)application {

//程序将要被激活时(获得焦点)执行,程序激活用户才能操作;

    // 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.

}


- (void)applicationDidEnterBackground:(UIApplication *)application {

//程序进入后台后执行,注意进入后台时会先失去焦点再进入后台;

    // 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.

}


- (void)applicationWillEnterForeground:(UIApplication *)application {

//程序将要进入前台时执行;

    // 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.

}


- (void)applicationDidBecomeActive:(UIApplication *)application {

//程序被激活(获得焦点)后执行,注意程序被激活时会先进入前台再被激活;

    // 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.

}


- (void)applicationWillTerminate:(UIApplication *)application {

//程序在终止时执行,包括正常终止或异常终止,例如说一个应用程序在后台运行(例如音乐播放软件、社交软件等)占用太多内存这时会意外终止调用此方法;

    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

}


@end




0 0
原创粉丝点击