UIWindow的常用方法。makeKeyWindow、makeKeyAndVisible、获取当前应用的主窗口和所有窗口
2017-10-29 11:27
567 查看
UIWindow的常用方法:
- (void)makeKeyWindow;
让当前UIWindow变成keyWindow(主窗口)
- (void)makeKeyAndVisible;
让当前UIWindow变成keyWindow,并显示出来
UIWindow的获得:
[UIApplicationsharedApplication].windows
获取当前应用的所有的UIWindow
[UIApplicationsharedApplication].keyWindow
获取当前应用的主窗口
view.window
获得某个UIView所在的UIWindow
下面是一个demo,用来测试UIWindow:
1,新建一个没有main.toryboard的工程
2,在delegate的didFinishLaunchingWithOptions:方法中创建UIWindow:
来自CODE的代码片
JLAppDelegate.h
来自CODE的代码片
JLAppDelegate.m
3,效果图:
3.1,从代码中我们可以得知,红色部分的window不是主窗口,蓝色部分的window是主窗口。
3.2,在ios7.1的模拟器中,主窗口和非主窗口中的输入框都能输入文字,但是在ios6.1的模拟器中,非主窗口的输入框不能输入文字。
注意:如果有时候发现文本输入框不能输入文字,那就有可能是因为该文本输入框所处的UIWindow不是keyWindow(主窗口)
3.3,弹出的键盘处在一个新的UIWindow中,也就是说键盘弹出后,这个程序就有了3个UIWindow。
如何证实键盘处在一个新的UIWindow中呢?在弹出键盘之前,通过[UIApplication sharedApplication].windows方法获取到所有的UIWindow,我们可以在主窗口中弄一个按钮,并给按钮设置监听(假设监听方法是btnClick方法),弹出键盘之后,在btnClick方法中通过[UIApplication sharedApplication].windows方法获取到所有的UIWindow,比较之前获取到的所有的UIWindow就可以证明了。
- (void)makeKeyWindow;
让当前UIWindow变成keyWindow(主窗口)
- (void)makeKeyAndVisible;
让当前UIWindow变成keyWindow,并显示出来
UIWindow的获得:
[UIApplicationsharedApplication].windows
获取当前应用的所有的UIWindow
[UIApplicationsharedApplication].keyWindow
获取当前应用的主窗口
view.window
获得某个UIView所在的UIWindow
下面是一个demo,用来测试UIWindow:
1,新建一个没有main.toryboard的工程
2,在delegate的didFinishLaunchingWithOptions:方法中创建UIWindow:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | //// JLAppDelegate.h// 01-UIWindow//// Created by XinYou on 15-3-11.// Copyright (c) 2015ĺš´ vxinyou. All rights reserved.// #import <UIKit/UIKit.h> @interface JLAppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @property (strong, nonatomic) UIWindow *window2; @end |
JLAppDelegate.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | //// JLAppDelegate.m// 01-UIWindow//// Created by XinYou on 15-3-11.// Copyright (c) 2015年 vxinyou. All rights reserved.// #import "JLAppDelegate.h" @implementation JLAppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ // 第1个window self.window = [[UIWindow alloc] init]; self.window.frame = [UIScreen mainScreen].bounds; self.window.backgroundColor = [UIColor redColor]; // 让window成为keyWindow(主窗口),默认不可见// [self.window makeKeyWindow]; // 让window成为keyWindow(主窗口),并且可见 [self.window makeKeyAndVisible]; // 给第1个window添加一个输入框 UITextField *tf = [[UITextField alloc] init]; tf.frame = CGRectMake(10, 10, 100, 30); tf.borderStyle = UITextBorderStyleRoundedRect; [self.window addSubview:tf]; // 打印当前应用的主窗口,此时应用的主窗口是window NSLog(@"%p", [UIApplication sharedApplication].keyWindow); // 第2个window self.window2 = [[UIWindow alloc] initWithFrame:CGRectMake(100, 100, 200, 200)]; self.window2.backgroundColor = [UIColor blueColor]; // 让window2成为keyWindow(主窗口),并且可见。此时window将不再是主窗口,但是仍然能显示 [self.window2 makeKeyAndVisible]; // 给第2个window添加一个输入框 UITextField *tf2 = [[UITextField alloc] init]; tf2.frame = CGRectMake(50, 50, 70, 40); tf2.borderStyle = UITextBorderStyleRoundedRect; [self.window2 addSubview:tf2]; // 打印当前应用的主窗口,此时window2已经成为了应用的主窗口 NSLog(@"%p", [UIApplication sharedApplication].keyWindow); 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 |
JLAppDelegate.m
3,效果图:
3.1,从代码中我们可以得知,红色部分的window不是主窗口,蓝色部分的window是主窗口。
3.2,在ios7.1的模拟器中,主窗口和非主窗口中的输入框都能输入文字,但是在ios6.1的模拟器中,非主窗口的输入框不能输入文字。
注意:如果有时候发现文本输入框不能输入文字,那就有可能是因为该文本输入框所处的UIWindow不是keyWindow(主窗口)
3.3,弹出的键盘处在一个新的UIWindow中,也就是说键盘弹出后,这个程序就有了3个UIWindow。
如何证实键盘处在一个新的UIWindow中呢?在弹出键盘之前,通过[UIApplication sharedApplication].windows方法获取到所有的UIWindow,我们可以在主窗口中弄一个按钮,并给按钮设置监听(假设监听方法是btnClick方法),弹出键盘之后,在btnClick方法中通过[UIApplication sharedApplication].windows方法获取到所有的UIWindow,比较之前获取到的所有的UIWindow就可以证明了。
相关文章推荐
- ios-day11-01(UIWindow的常用方法。makeKeyWindow、makeKeyAndVisible、获取当前应用的主窗口和所有窗口)
- ios-day11-01(UIWindow的常用方法。makeKeyWindow、makeKeyAndVisible、获取当前应用的主窗口和所有窗口)
- C# Windows API应用之基于GetDesktopWindow获得桌面所有窗口句柄的方法
- C# Windows API应用之GetDesktopWindow ——获得桌面所有窗口句柄的方法
- VC Windows API应用之GetDesktopWindow ——获得桌面所有窗口句柄的方法
- iOS 【程序启动原理/手动创建UIWindow/UIApplicationMain做了些什么/makeKeyAndVisible底层实现】
- 获取当前窗口标题的方法
- 解决Firefox中有时使用window.close()脚本无法关闭当前窗口的方法
- 【整理】ASP.NET获取当前时间的所有方法
- makeKeyAndVisible
- 获取当前运行的fsMDIChild窗口和所有fsMDIChild窗体
- js中两个常用方法(1.获取当前工程的虚拟目录;2.移除数组指定位置上的元素)
- 获取系统中所有应用的方法
- js获取对象,数组所有属性值(key)和对应值(value)的方法
- 获取当前窗口的所有按钮控件,并打印其状态
- C#中获取程序当前路径的7种最常用的方法
- makeKeyAndVisible
- 【整理】ASP.NET获取当前时间的所有方法
- VC++中,获取窗口句柄的常用方法
- 【整理】ASP.NET获取当前时间的所有方法