码迷,mamicode.com
首页 > 其他好文 > 详细

程序启动的完整过程

时间:2015-07-09 00:24:26      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:

程序启动的完整过程

 int main(int argc, char * argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([MJAppDelegate class]));
    }
}

执行顺序

  • 1.main函数

  • 2.UIApplicationMain

    • 创建UIApplication对象
    • 创建UIApplication的delegate对象
  • 3.开启主运行循环
  • 3(1).delegate对象开始处理(监听)系统事件(没有storyboard)

    • 程序启动完毕的时候,就会调用代理的application:didFinishLaunchingWithOptions:方法
    • 在application:didFinishLaunchingWithOptions:中创建UIWindow
    • 创建和设置UIWindow的rootViewController
    • 显示窗口
  • 3<1>.根据Info.plist获得最主要storyboard的文件名,加载最主要的storyboard(有storyboard)

  • 创建UIWindow
  • 创建和设置UIWindow的rootViewController
  • 显示窗口

窗口(UIWindow)

  • 一种特殊的UIView,一个app中至少有一个UIWindow
  • iOS启动完毕后创建的第一个视图就是UIWindow
  • 接着创建控制器,再把控制器的View加到UIWindow上显示出来

- didFinishLaunchingWithOptions:方法中代码创建控制器

//创建窗口
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
//创建跟控制器
UIViewController *VC = [[UIViewController alloc] init];
//加到窗口的跟控制器中
self.window.rootViewController = VC;
//把控制器的View加到窗口中,,不加到跟控制器中也可以显示
//[self.window addSubview:rootVc.view];
//显示窗口
//[self.window makeKeyWindow];//设为主窗口
[self.window makeKeyAndVisible];//设为主窗口且显示
//self.window.hidden = NO;//只显示

窗口不能显示的原因

  • 没有强引用窗口
  • 没设置窗口的尺寸
  • 控制器没加到窗口中或者设为跟控制器

- didFinishLaunchingWithOptions:方法中代码加载storyboard的控制器

    // 1.创建窗口
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

    // 2.加载storyboard文件,创建控制器

    // name:就是storyboard文件名
    // bundle:主bundle,传入nil,表示主bundle
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

    // 通过标识创建控制器
//    UIViewController *rootVc = [storyboard instantiateViewControllerWithIdentifier:@"vc"];

    // instantiateInitialViewController:加载storyboard箭头指向的控制器
    UIViewController *rootVc = [storyboard instantiateInitialViewController];

    // 3.设置窗口的根控制器,并且显示窗口
    self.window.rootViewController = rootVc;

    // 4.显示窗口
    [self.window makeKeyAndVisible];

- didFinishLaunchingWithOptions:方法中代码加载xib的控制器

    // 1.创建窗口
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

    // 2.设置窗口的根控制器
    // 通过xib去加载控制器
    // <1>.首先创建一个Xib文件
    // <2>.Xib文件需要拖一个View描述控制器的View
    // <3>.需要把Xib上的View与控制器连线,设置Xib的File‘owner为控制器
    UIViewController *rootVc = [[UIViewController alloc] initWithNibName:@"VC" bundle:nil];
    self.window.rootViewController = rootVc;

    // 3.显示窗口
    [self.window makeKeyAndVisible];

加载xib文件的顺序

  • 若加载xib文件传入name参数,则按参数描述的加载
  • 若未传入参数
    • 先加载与控制器名字的View.xib
      • 若控制器命名为YLViewController,则首先加载YLView.xib
    • 而后加载与控制名字一样的xib
      • 若控制器命名为YLViewController,则首先加载YLViewContrller.xib
    • 若都找不到则加载空。

创建控制器View的顺序

  • 优先layoutView代码创建
  • storyboard创建
  • xib创建

技术分享

控制器View的生命周期

  • loadView
  • viewDidLoad(view加载完毕)
  • viewWillAppear(view即将显示到window)
  • viewDidAppear(view显示完毕,已经显示到窗口)
  • viewWillDisappear(view即将从window上移除,即将看不到)
  • viewDidDisappear(view从window上完全移除)

    当有内存警告时

  • didReceiveMemoryWarning(当收到内存警告)
  • viewWillUnload(view即将销毁的时候)

    销毁view,首先不在视野中得view

  • viewDidUnload(view销毁完毕)

处理应用程序的一些生命周期方法

// 程序启动完成的时候调用
// __func__:表示当前的方法在哪个类里面调用
// 程序加载完毕的时候调用
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    return YES;
}

// 当应用程序失去焦点的时候调用
- (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:.
}

// 当程序接收到内存警告的时候调用
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
    // 可以在此处清空图片缓存
    NSLog(@"%s",__func__);
}

程序启动的完整过程

标签:

原文地址:http://www.cnblogs.com/ShaoYinling/p/4631559.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!