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

相关文章

PostgreSQL中rank()窗口函数实用指南与示例

《PostgreSQL中rank()窗口函数实用指南与示例》在数据分析和数据库管理中,经常需要对数据进行排名操作,PostgreSQL提供了强大的窗口函数rank(),可以方便地对结果集中的行进行排名... 目录一、rank()函数简介二、基础示例:部门内员工薪资排名示例数据排名查询三、高级应用示例1. 每

nginx -t、nginx -s stop 和 nginx -s reload 命令的详细解析(结合应用场景)

《nginx-t、nginx-sstop和nginx-sreload命令的详细解析(结合应用场景)》本文解析Nginx的-t、-sstop、-sreload命令,分别用于配置语法检... 以下是关于 nginx -t、nginx -s stop 和 nginx -s reload 命令的详细解析,结合实际应

Java中读取YAML文件配置信息常见问题及解决方法

《Java中读取YAML文件配置信息常见问题及解决方法》:本文主要介绍Java中读取YAML文件配置信息常见问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要... 目录1 使用Spring Boot的@ConfigurationProperties2. 使用@Valu

Java 方法重载Overload常见误区及注意事项

《Java方法重载Overload常见误区及注意事项》Java方法重载允许同一类中同名方法通过参数类型、数量、顺序差异实现功能扩展,提升代码灵活性,核心条件为参数列表不同,不涉及返回类型、访问修饰符... 目录Java 方法重载(Overload)详解一、方法重载的核心条件二、构成方法重载的具体情况三、不构

SQL中如何添加数据(常见方法及示例)

《SQL中如何添加数据(常见方法及示例)》SQL全称为StructuredQueryLanguage,是一种用于管理关系数据库的标准编程语言,下面给大家介绍SQL中如何添加数据,感兴趣的朋友一起看看吧... 目录在mysql中,有多种方法可以添加数据。以下是一些常见的方法及其示例。1. 使用INSERT I

Python中反转字符串的常见方法小结

《Python中反转字符串的常见方法小结》在Python中,字符串对象没有内置的反转方法,然而,在实际开发中,我们经常会遇到需要反转字符串的场景,比如处理回文字符串、文本加密等,因此,掌握如何在Pyt... 目录python中反转字符串的方法技术背景实现步骤1. 使用切片2. 使用 reversed() 函

Python中将嵌套列表扁平化的多种实现方法

《Python中将嵌套列表扁平化的多种实现方法》在Python编程中,我们常常会遇到需要将嵌套列表(即列表中包含列表)转换为一个一维的扁平列表的需求,本文将给大家介绍了多种实现这一目标的方法,需要的朋... 目录python中将嵌套列表扁平化的方法技术背景实现步骤1. 使用嵌套列表推导式2. 使用itert

Python使用pip工具实现包自动更新的多种方法

《Python使用pip工具实现包自动更新的多种方法》本文深入探讨了使用Python的pip工具实现包自动更新的各种方法和技术,我们将从基础概念开始,逐步介绍手动更新方法、自动化脚本编写、结合CI/C... 目录1. 背景介绍1.1 目的和范围1.2 预期读者1.3 文档结构概述1.4 术语表1.4.1 核

在Linux中改变echo输出颜色的实现方法

《在Linux中改变echo输出颜色的实现方法》在Linux系统的命令行环境下,为了使输出信息更加清晰、突出,便于用户快速识别和区分不同类型的信息,常常需要改变echo命令的输出颜色,所以本文给大家介... 目python录在linux中改变echo输出颜色的方法技术背景实现步骤使用ANSI转义码使用tpu

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

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