UIWindow的常用方法。makeKeyWindow、makeKeyAndVisible、获取当前应用的主窗口和所有窗口

本文主要是介绍UIWindow的常用方法。makeKeyWindow、makeKeyAndVisible、获取当前应用的主窗口和所有窗口,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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:

  1234567891011121314151617
           
//
// 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
来自CODE的代码片
JLAppDelegate.h
  12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
           
//
// 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
来自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就可以证明了。

这篇关于UIWindow的常用方法。makeKeyWindow、makeKeyAndVisible、获取当前应用的主窗口和所有窗口的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/852051

相关文章

Conda与Python venv虚拟环境的区别与使用方法详解

《Conda与Pythonvenv虚拟环境的区别与使用方法详解》随着Python社区的成长,虚拟环境的概念和技术也在不断发展,:本文主要介绍Conda与Pythonvenv虚拟环境的区别与使用... 目录前言一、Conda 与 python venv 的核心区别1. Conda 的特点2. Python v

Spring Boot中WebSocket常用使用方法详解

《SpringBoot中WebSocket常用使用方法详解》本文从WebSocket的基础概念出发,详细介绍了SpringBoot集成WebSocket的步骤,并重点讲解了常用的使用方法,包括简单消... 目录一、WebSocket基础概念1.1 什么是WebSocket1.2 WebSocket与HTTP

Python使用OpenCV实现获取视频时长的小工具

《Python使用OpenCV实现获取视频时长的小工具》在处理视频数据时,获取视频的时长是一项常见且基础的需求,本文将详细介绍如何使用Python和OpenCV获取视频时长,并对每一行代码进行深入解析... 目录一、代码实现二、代码解析1. 导入 OpenCV 库2. 定义获取视频时长的函数3. 打开视频文

PostgreSQL的扩展dict_int应用案例解析

《PostgreSQL的扩展dict_int应用案例解析》dict_int扩展为PostgreSQL提供了专业的整数文本处理能力,特别适合需要精确处理数字内容的搜索场景,本文给大家介绍PostgreS... 目录PostgreSQL的扩展dict_int一、扩展概述二、核心功能三、安装与启用四、字典配置方法

SQL Server配置管理器无法打开的四种解决方法

《SQLServer配置管理器无法打开的四种解决方法》本文总结了SQLServer配置管理器无法打开的四种解决方法,文中通过图文示例介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录方法一:桌面图标进入方法二:运行窗口进入检查版本号对照表php方法三:查找文件路径方法四:检查 S

MyBatis-Plus 中 nested() 与 and() 方法详解(最佳实践场景)

《MyBatis-Plus中nested()与and()方法详解(最佳实践场景)》在MyBatis-Plus的条件构造器中,nested()和and()都是用于构建复杂查询条件的关键方法,但... 目录MyBATis-Plus 中nested()与and()方法详解一、核心区别对比二、方法详解1.and()

golang中reflect包的常用方法

《golang中reflect包的常用方法》Go反射reflect包提供类型和值方法,用于获取类型信息、访问字段、调用方法等,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值... 目录reflect包方法总结类型 (Type) 方法值 (Value) 方法reflect包方法总结

C# 比较两个list 之间元素差异的常用方法

《C#比较两个list之间元素差异的常用方法》:本文主要介绍C#比较两个list之间元素差异,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. 使用Except方法2. 使用Except的逆操作3. 使用LINQ的Join,GroupJoin

MySQL查询JSON数组字段包含特定字符串的方法

《MySQL查询JSON数组字段包含特定字符串的方法》在MySQL数据库中,当某个字段存储的是JSON数组,需要查询数组中包含特定字符串的记录时传统的LIKE语句无法直接使用,下面小编就为大家介绍两种... 目录问题背景解决方案对比1. 精确匹配方案(推荐)2. 模糊匹配方案参数化查询示例使用场景建议性能优

关于集合与数组转换实现方法

《关于集合与数组转换实现方法》:本文主要介绍关于集合与数组转换实现方法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、Arrays.asList()1.1、方法作用1.2、内部实现1.3、修改元素的影响1.4、注意事项2、list.toArray()2.1、方