美文网首页执行顺序
iOS 执行顺序-- AppDelegate及 UIViewC

iOS 执行顺序-- AppDelegate及 UIViewC

作者: 9d8c8692519b | 来源:发表于2018-12-11 11:27 被阅读17次

之前一直忙着做项目,最近在看一些很基础的东西。跟之前看的时候感觉完全不一样了。

一、iOS程序的启动执行顺序

1 程序的入口

进入main函数, 设置AppDelegate称为函数的代理

2 程序完成加载

 -[AppDelegate application:didFinishLaunchingWithOptions:]

3 创建window窗口

4 程序被激活

-[AppDelegate applicationDidBecomeActive:]

5 当点击command+H时

程序取消激活状态

-[AppDelegate applicationWillResignActive:]

程序进入后台

-[AppDelegate applicationDidEnterBackground:]

6 点击进入工程

程序进入前台

-[AppDelegate applicationWillEnterForeground:]

程序被激活

-[AppDelegate applicationDidBecomeActive:]

1.1: AppDelegate类实现文件

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOption{// Override point for customization after application launch.
    NSLog(@"didFinishLaunchingWithOptions");
    return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application { /* 当应用程序从活动状态(active)变到非活动状态(inactive时被触发调用, 这可能发生在一些临时中断下(例如:来电话、来短信)又或者程序退出时,他会先过渡到后台然后terminate 使用这方法去暂停正在进行的任务,禁用计时器,节流OpenGL ES 帧率。在游戏中应该在这个方法里面暂停游戏。 */
    // 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.
    NSLog(@"WillResignActive");
}

- (void)applicationDidEnterBackground:(UIApplication *)application { /* 使用这种方法来释放共享资源,保存用户数据,无效计时器,存储足够多的应用程序状态信息来恢复您的应用程序的当前状态,以防它终止丢失数据。 如果你的程序支持后台运行,那么当用户退出时不会调用applicationWillTerminate。 */
    // 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.
        NSLog(@"DidEnterBackground");
    }

- (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.
    NSLog(@"WillEnterForeground");
}

- (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.
        NSLog(@"DidBecomeActive");
    }

- (void)applicationWillTerminate:(UIApplication *)application { /* 终止,game over */
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    NSLog(@"WillTerminate");
}

1.2: 分析:

1.application:didFinishLaunchingWithOptions:
程序首次已经完成启动时执行,若直接启动,launchOptions中没有数据;否则,launchOptions将包含对应方式的内容(比如从微信中启动节奏大师--)。

2.applicationWillResignActive(非活动)
程序将要失去Active状态时调用,比如按下Home键或有电话信息进来。之后程序将进入后台状态。对应的applicationWillEnterForeground这个方法用来
a、暂停正在执行的任务;
b、禁止计时器;
c、减少OpenGL ES帧率;
d、若为游戏应暂停游戏;

3.applicationDidEnterBackground(已经进入后台)
程序已经进入后台时调用,对应applicationDidBecomeActive(已经变成前台),这个方法用来
a、释放共享资源;
b、保存用户数据(写到硬盘);
c、作废计时器;
d、保存足够的程序状态以便下次恢复;

4.applicationWillEnterForeground(将进入前台)
程序即将进去前台时调用,对应applicationWillResignActive(将进入后台)。这个方法用来
1.撤销applicationWillResignActive中做的改变。

5.applicationDidBecomeActive(已经进入前台)
程序已经变为Active(前台)时调用。对应applicationDidEnterBackground(已经进入后台)。
1.若程序之前在后台,在此方法内刷新用户界面。

6.applicationWillTerminate
程序即将退出时调用。记得保存数据,如applicationDidEnterBackground方法一样。

二、 UIViewController的生命周期

// The designated initializer //这个UIViewController的指定初始化方法(其他的初始化方法最终要调用这个初始化方法); //如果连接了串联图storyBoard根本就不用管这货
// 这里需哟注意的是,如果是代码创建。会调用的是:- (void)loadView,系统默认会创建一个空白的View,定制View可以重写该方法
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    NSLog(@"%s", __FUNCTION__);
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
        return self;
    }
    
//视图控制器中的视图加载完成,viewController自带的view加载完成
- (void)viewDidLoad {
    NSLog(@"%s", __FUNCTION__);
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

//出现内存警告  //模拟内存警告:点击模拟器->hardware-> Simulate Memory Warning
- (void)didReceiveMemoryWarning {
    NSLog(@"%s", __FUNCTION__);
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//视图将要出现
- (void)viewWillAppear:(BOOL)animated {
    NSLog(@"%s", __FUNCTION__);
    [super viewWillAppear:animated];
}

//视图已经出现
- (void)viewDidAppear:(BOOL)animated {
    NSLog(@"%s", __FUNCTION__);
    [super viewDidAppear:animated];
}

//视图将要消失 //双击Home键,向上推出程序执行该函数
- (void)viewWillDisappear:(BOOL)animated {
    NSLog(@"%s", __FUNCTION__);
    [super viewWillDisappear:animated];
}

//视图已经消失
- (void)viewDidDisappear:(BOOL)animated {
    NSLog(@"%s", __FUNCTION__);
    [super viewDidDisappear:animated];
}
@end

相关文章

网友评论

    本文标题:iOS 执行顺序-- AppDelegate及 UIViewC

    本文链接:https://www.haomeiwen.com/subject/chjshqtx.html