iOS应用程序启动流程
- 1.打开程序
- 2.执行main函数
- 3.执行UIApplicationMain函数
- 4.初始化UIApplication(创建和设置代理对象,开启事件循环)
- 5.监听系统事件
application:didFinishLaunchingWithOptions:
applicationDidBecomeActive:
applicationDidEnterBackground:
applicationWillResignActivie:
applicationWillEnterForeground:
applicationDidReceiveMemoryWarning:
applicationWillTerminate:
程序启动完成的时候调用
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
NSLog(@"%s",__func__);
return YES;
}
当app失去焦点的时候调用
- (void)applicationWillResignActive:(UIApplication *)application {
NSLog(@"%s",__func__);
// 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.
}
app进入后台的时候调用,app忽然打断的时候,在这里保存一些需要用到的数据
- (void)applicationDidEnterBackground:(UIApplication *)application {
NSLog(@"%s",__func__);
// 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.
}
app进入即将前台
- (void)applicationWillEnterForeground:(UIApplication *)application {
NSLog(@"%s",__func__);
// 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.
}
当app获取到焦点的时候调用,意味着app可以与用户交互
- (void)applicationDidBecomeActive:(UIApplication *)application {
NSLog(@"%s",__func__);
// 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.
}
app被关闭的时候调用
- (void)applicationWillTerminate:(UIApplication *)application {
NSLog(@"%s",__func__);
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
app接收到内存警告的时候调用 用途 清空图片的缓存
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
NSLog(@"%s",__func__);
}
Window 的层级关系
UIWindowLevelNormal < UIWindowLevelStatusBar < UIWindowLevelAlert
makeKeyAndVisible作用 : 成为app的主窗口并且显示
[self.window makeKeyAndVisible];
- 整个app中只有一个UIApplication
- UIApplication一般用来做一些应用级别的操作(app的提醒框,联网状态,打电话,打开网页,控制状态栏)
// 设置appIcon提醒数字,必须注册用户通知
app.applicationIconBadgeNumber = 10;
// 创建用户通知
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil];
// 注册用户的通知
[app registerUserNotificationSettings:settings];
// 设置联网状态
app.networkActivityIndicatorVisible = YES;
#pragma mark - 控制器设置状态栏
//在iOS7以后,状态栏默认由控制器决定
//隐藏状态栏
- (BOOL)prefersStatusBarHidden
{
return YES;
}
- (UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}
// 程序启动完成的时候
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// 1.创建窗口,注意窗口必须要有尺寸,尺寸跟屏幕一样大的尺寸,窗口不要被释放
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor redColor];
// 2.创建窗口的跟控制器
UIViewController *vc = [[UIViewController alloc] init];
vc.view.backgroundColor = [UIColor yellowColor];
[vc.view addSubview:[UIButton buttonWithType:UIButtonTypeContactAdd]];
// 如果设置窗口的跟控制器,默认就会把控制器的view添加到窗口上
// 设置窗口的跟控制器,默认就有旋转功能
self.window.rootViewController = vc;
// [self.window addSubview:vc.view];
// 3.显示窗口
[self.window makeKeyAndVisible];
return YES;
}
通过storyboard创建控制器的步骤:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// 创建窗口
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
// 创建窗口的跟控制器
// 加载storyboard
// storyboard文件名,不需要带后缀
// nil: [NSBundle mainBundle]
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
// 通过storyboard创建控制器
// instantiateInitialViewController:加载箭头指向的控制器
UIViewController *vc = [storyboard instantiateInitialViewController];
UIViewController *vc2 = [storyboard instantiateViewControllerWithIdentifier:@"green"];
//
NSLog(@"%@",[vc class]);
self.window.rootViewController = vc;
// 显示窗口
[self.window makeKeyAndVisible];
return YES;
}
通过 Xib 创建控制器的步骤
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// 创建窗口
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
// 通过xib创建控制器
ViewController *vc = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
self.window.rootViewController = vc;
[self.window makeKeyAndVisible];
return YES;
}
loadView 作用
// loadView作用:自定义控制器的view
// loadView什么时候调用:第一次使用控制器的view的时候调用
// 注意:在这个方法中如果没有自定义view,就不能获取控制器的view
// 一旦重写了这个方法,就不要调用[super loadView]
// 如果重写了这个方法,就不会去加载storyboard描述的控制器的View
- (void)loadView
{
// 创建控制器view
self.view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.view.backgroundColor = [UIColor purpleColor];
}
loadView直接不实现就是系统默认的做法
- (void)loadView
{
// super -> UIViewController
// 系统默认的做法,一定不要这样写
[super loadView];
}
控制器 View 的创建(Xib)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
// 加载跟类名相同的xib
// 如果描述控制器View的xib跟控制器的类名相同,就会去加载
// 只有控制器的init方法底层会调用initWithNibName:bundle:
// 只要通过initWithNibName:bundle:初始化控制器,并且nibName为nil,就会执行以下几步。
// XMGViewController类型
// 1.寻找有没有跟控制器类名同名但是不带Controller的xib,如果有就会去加载(XMGView.xib)
// 2.寻找有没有跟控制器类名同名的xib,如果有就会去加载(XMGViewController.xib)
// 3.如果都没有找到,创建空的view,
UIViewController *vc = [[ViewController alloc] initWithNibName:@"VC" bundle:nil];
self.window.rootViewController = vc;
[self.window makeKeyAndVisible];
return YES;
}
控制器 View 的懒加载
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 创建窗口
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
// 创建UIViewController控制器,控制器的view并没有创建
// 控制器的view懒加载:第一次使用的时候才会去加载,并不是创建UIViewController控制器的时候去加载
UIViewController *vc = [[ViewController alloc] init];
vc.view.backgroundColor = [UIColor redColor];
self.window.rootViewController = vc;
// 显示窗口
[self.window makeKeyAndVisible];
return YES;
}
- (void)loadView
{
// 如果控制器是窗口的根控制器就可以不用设置尺寸
self.view = [[UIView alloc] initWithFrame:CGRectZero];
self.view.backgroundColor = [UIColor yellowColor];
}
/**
* 底层实现原理
*/
- (UIView *)view
{
if (_view == nil) {
[self loadView];
[self viewDidLoad];
}
return _view;
}
// 在viewDidLoad中打印控制器的尺寸不准确,通常在viewDidAppear
// 控制器的view加载完成的时候调用
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor blueColor];
NSLog(@"%s",__func__);
}
// view完全显示的时候调用
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
NSLog(@"%@",NSStringFromCGRect(self.view.bounds));
}
导航控制器的基本使用
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// 创建窗口
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
// 创建导航控制器的跟控制器,也属于导航控制器的子控制器
UIViewController *vc = [[OneViewController alloc] init];
vc.view.backgroundColor = [UIColor redColor];
// 导航控制器也需要一个根控制器
// 默认导航控制器把根控制器的view添加到导航控制器的view上
UINavigationController *navVc = [[UINavigationController alloc] initWithRootViewController:vc];
NSLog(@"%@",navVc);
// 设置窗口的跟控制器
self.window.rootViewController = navVc;
[self.window makeKeyAndVisible];
return YES;
}
// pop不是马上把控制器销毁,而是等到动画结束再销毁
// 回到上一个界面
[self.navigationController popViewControllerAnimated:YES];
// 注意:只能返回到栈里面的控制器
[self.navigationController popToViewController:self.navigationController.childViewControllers[0] animated:YES];
// 返回的是根控制器
[self.navigationController popToRootViewControllerAnimated:YES];