zoukankan      html  css  js  c++  java
  • iOS main.m解析

    在iOS开发中,有一个文件main.m,可能并不是很引起开发的注意。不过,可能在面试过程中,面试官还是有些会问到主函数里面到底做了哪些工作和任务。下面我们主要看一下main.m内部的逻辑。

    #import <UIKit/UIKit.h>
    #import "AppDelegate.h"
    // 1.创建UIApplication对象
    // 2.创建AppDelegate对象,并且成为UIApplication对象代理属性
    // 3.开启主允许循环:目的让程序一直跑起来
    // 4.加载info.plist文件,判断下info.plist文件里面有木有指定main.storyboard,如果指定,就会去加载main.storyboard
    
    // main.storyboard
    // 1.初始化窗口
    // 2.加载storyboard文件,并且创建箭头指向的控制器
    // 3.把新创建的控制器作为窗口的跟控制器,让窗口现实
    int main(int argc, char * argv[]) {
        //    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        // instantiateInitialViewController:默认加载箭头指向的控制器
        //    [storyboard instantiateInitialViewController];
        
        @autoreleasepool {
            return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
        }
    }

    UIApplicationMain函数中创建一个UIApplication对象。每个iOS应用程序都会有且只有一个UIApplication对象,此对象也是单例,负责单例对象的维护和循环运行事件。程序中一旦创建了某个UIApplication单例对象,对象就会一直循环下去。

    通过查看上述int main()函数,发现UIApplicationMain还会创建某个指定类(也是AppDelegate)对象,并设置delegate;UIApplicationMain的第三个参数是NSString类型,代表是该对象所属的类。

    下面查看该类AppDelegate

    在实现文件中

    当应用程序运行的过程中,UIApplication单例对象会在应用出现变化时,调动不同delegate方法发出特定的消息,在实现文件中有以下方法:

    - (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;

    UIApplication负责建立程序的Event Loop,也就是事件循环。事件循环用于不断的接收用户的交互操作。

  • 相关阅读:
    对软件测试的理解
    Android 经典欧美小游戏 guess who
    Shell脚本 | 安卓应用权限检查
    自动化测试 | UI Automator 进阶指南
    Shell脚本 | 截取包名
    自动化测试 | UI Automator 入门指南
    杂谈随感 | 与测试无关
    Shell脚本 | 性能测试之内存
    Shell脚本 | 健壮性测试之空指针检查
    "java.lang.IllegalStateException: No instrumentation registered! Must run under a registering instrumentation."问题解决
  • 原文地址:https://www.cnblogs.com/guohai-stronger/p/9391288.html
Copyright © 2011-2022 走看看