UIApplication 应用对象

oc语言中 default* share* 开头的方法一般都是单例的。

每个应用只有一个这个对象的实例,也就是说,这个对象是单例的。UIApplication可以做一些跨应用的事情,比如使用浏览器打开网页。

获取
UIApplication *app = [UIApplication shareApplication];

使用

  1. 使用浏览器应用打开网页
    [app openURL:[NSURL URLString:@”http://www.baidu.com“]];
  2. 发送短信
  3. 发送邮件
  4. 状态栏操作:每个程序都有自己的状态栏。
  5. 操作应用图标上面的提示数字。
  6. 获取应用程序窗口对象

APPDelegate 应用程序代理对象

它是UIApplication应用程序的代理对象,可以监听整个应用程序的系统事件

比如:

  • 程序启动
  • 程序进入后台
  • 程序重新进入前台
  • 程序接收的内存警告
    ..
//程序启动完毕,就会调用一次(只调用一次)
- (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 invalidate graphics rendering callbacks. 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 active 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 {

}

当程序UIApplication对象监听的事件被触发,UIApplication对象会交给代理对象

程序启动原理

  1. main程序的入口,程序启动执行main函数
  2. main函数调用UIApplicationMain
    UIApplicationMain做的事情:
    1)创建UIApplication
    2)创建UIApplication代理对象
    3)开启一个消息循环(死循环)来监听用户所有操作,也就是这个函数不可能轻易返回,如果返回表示了程序可能关闭或者出现异常等。监听到系统事件时,会通知代理对象。
    4)为应用程序创建UIWindow对象,设置为MJAppDelegate的window属性。应用程序启动,我们看到的整个界面就是UIWindow对象。一个应用程序之所以能显示,是因为又UIWindow的对象,控制的view之所以界面上显示,是因为被添加到UIWindow对象上去了。如果没有这个对象,界面一片漆黑。
    5)加载info.plist文件
    6)加载主要的storyboard文件,创建白色箭头所指的控制器对象
    7)创建第6步的控制器(根控制器),设置控制器属性为根控制器。根控制器是程序启动时创建的控制器,被称为根控制器(rootController)
    8)UIWindow对象将rootController添加到自己的view上面,展示window。(在这一步才真正创建控制的view对象,因为view是延迟加载的。这个view的父窗口是UIWindow)
    [window addSubview : window.rootViewController.view];
  3. 程序显示

UIViewController

控制器内部view是延迟加载,只用在用到view的时候才加载。
view加载完毕会调用viewDidLoad方法。

创建控制器的方式

  • 代码创建
    MJOneViewController *one = [[MJOneViewController alloc] init];
  • 通过storyboard文件创建
    加载storyboard文件
    UIStoryboard *story = [UIStoryboard storyboardWithName:@”sb的文件名” bundle:nil];
    从storyboard中创建控制器
    根据控制器在storyboard文件中storyboard id来创建
    MJOneViewController *one = [story instantiateViewControllerWithIdentifier:@”控制器的storyboard id”];
    直接创建箭头所指的控制器(initial controller)
    MJOneViewController *one = [story instantiateInitialViewController];
  • 通过xib创建控制器(xib是storyboard的前身)
    创建控制器的view时,就会加载MJOneViewController.xib文件,并且会将控制器当做是xib的Owner传入
    MJOneViewController *one = [[MJOneViewController alloc] initWithNibName:@”MJOneViewController” bundle:nil];
    加载one控制器的view:[[NSBunle mainBundle] loadNibName:@”MJOneViewController” owner:one options:nil];