直接将view添加到UIWindow中,但并不会理会view对应的UIViewController
自动将rootViewController的view添加到UIWindow中,负责管理rootViewController的生命周期
让当前UIWindow变成keyWindow(主窗口)
让当前UIWindow变成keyWindow,并显示出来
在本应用中打开的UIWindow列表,这样就可以接触应用中的任何一个UIView对象
(平时输入文字弹出的键盘,就处在一个新的UIWindow中)
用来接收键盘以及非触摸类的消息事件的UIWindow,而且程序中每个时刻只能有一个UIWindow是keyWindow。如果某个UIWindow内部的文本框不能输入文字,可能是因为这个UIWindow不是keyWindow
获得某个UIView所在的UIWindow
// // MJAppDelegate.m // #import "MJAppDelegate.h" #import "MjOneViewController.h" @implementation MJAppDelegate // 旋转事件 --> UIApplication --> UIWindow /** * 程序启动完毕就会调用一次 */ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // 1.创建window self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // 2.设置window的背景色 self.window.backgroundColor = [UIColor whiteColor]; MjOneViewController *one = [[MjOneViewController alloc] init]; //[self.window addSubview:one.view]; 这种方式添加 会产生野指针错误 one是局部变量 而one.view是随window存在 self.window.rootViewController = one; //把控制器添加到window上的 第2种方法 等同于上面两行代码 并且会自动把view添加到控制器上面 // 3.显示window [self.window makeKeyAndVisible]; 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:. } @end
#import "MJAppDelegate.h" @implementation MJAppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // 1.创建窗口 self.window = [[UIWindow alloc] init]; self.window.frame = [UIScreen mainScreen].bounds; //window的尺寸就是整个屏幕的尺寸 // 2.设置颜色 self.window.backgroundColor = [UIColor redColor]; // 3.显示窗口 // 让window成为keyWindow(主窗口) // [self.window makeKeyWindow]; // 让window成为keyWindow(主窗口)\并且可见 [self.window makeKeyAndVisible]; // self.window = window; UITextField *tf = [[UITextField alloc] init]; tf.frame = CGRectMake(10, 10, 100, 30); tf.borderStyle = UITextBorderStyleRoundedRect; [self.window addSubview:tf]; // 第2个窗口 self.window2 = [[UIWindow alloc] init]; self.window2.frame = CGRectMake(100, 100, 200, 200); self.window2.backgroundColor = [UIColor yellowColor]; [self.window2 makeKeyAndVisible]; UITextField *tf2 = [[UITextField alloc] init]; tf2.frame = CGRectMake(50, 50, 70, 40); tf2.borderStyle = UITextBorderStyleRoundedRect; [self.window2 addSubview:tf2]; UIButton *btn = [UIButton buttonWithType:UIButtonTypeContactAdd]; [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside]; [self.window2 addSubview:btn]; return YES; } /** * 要改键盘上的某个东西 就要拿到键盘所在的那个窗口 这样就能拿到里面的子空件 就可以拿到键盘了 */ - (void)btnClick { NSLog(@"%@", [UIApplication sharedApplication].windows); } - (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:. } @end