xib创建
// 创建窗口
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
// 通过xib创建控制器
ViewController *vc = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
self.window.rootViewController = vc;
[self.window makeKeyAndVisible];
```
## storyboard创建
```objc
// 创建窗口
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 *vc = [storyboard instantiateViewControllerWithIdentifier:@"green"];
NSLog(@"%@",[vc class]);
self.window.rootViewController = vc;
// 显示窗口
[self.window makeKeyAndVisible];
代码创建
// 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];
```
window是有层级的,而且不是只有一个
AppDelegate
```objc
// 程序启动完成的时候调用
- (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__);
}