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

相关文章

vue基于ElementUI动态设置表格高度的3种方法

《vue基于ElementUI动态设置表格高度的3种方法》ElementUI+vue动态设置表格高度的几种方法,抛砖引玉,还有其它方法动态设置表格高度,大家可以开动脑筋... 方法一、css + js的形式这个方法需要在表格外层设置一个div,原理是将表格的高度设置成外层div的高度,所以外层的div需要

5分钟获取deepseek api并搭建简易问答应用

《5分钟获取deepseekapi并搭建简易问答应用》本文主要介绍了5分钟获取deepseekapi并搭建简易问答应用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需... 目录1、获取api2、获取base_url和chat_model3、配置模型参数方法一:终端中临时将加

Python判断for循环最后一次的6种方法

《Python判断for循环最后一次的6种方法》在Python中,通常我们不会直接判断for循环是否正在执行最后一次迭代,因为Python的for循环是基于可迭代对象的,它不知道也不关心迭代的内部状态... 目录1.使用enuhttp://www.chinasem.cnmerate()和len()来判断for

JavaScript中的isTrusted属性及其应用场景详解

《JavaScript中的isTrusted属性及其应用场景详解》在现代Web开发中,JavaScript是构建交互式应用的核心语言,随着前端技术的不断发展,开发者需要处理越来越多的复杂场景,例如事件... 目录引言一、问题背景二、isTrusted 属性的来源与作用1. isTrusted 的定义2. 为

Java循环创建对象内存溢出的解决方法

《Java循环创建对象内存溢出的解决方法》在Java中,如果在循环中不当地创建大量对象而不及时释放内存,很容易导致内存溢出(OutOfMemoryError),所以本文给大家介绍了Java循环创建对象... 目录问题1. 解决方案2. 示例代码2.1 原始版本(可能导致内存溢出)2.2 修改后的版本问题在

四种Flutter子页面向父组件传递数据的方法介绍

《四种Flutter子页面向父组件传递数据的方法介绍》在Flutter中,如果父组件需要调用子组件的方法,可以通过常用的四种方式实现,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录方法 1:使用 GlobalKey 和 State 调用子组件方法方法 2:通过回调函数(Callb

一文详解Python中数据清洗与处理的常用方法

《一文详解Python中数据清洗与处理的常用方法》在数据处理与分析过程中,缺失值、重复值、异常值等问题是常见的挑战,本文总结了多种数据清洗与处理方法,文中的示例代码简洁易懂,有需要的小伙伴可以参考下... 目录缺失值处理重复值处理异常值处理数据类型转换文本清洗数据分组统计数据分箱数据标准化在数据处理与分析过

Java中Object类的常用方法小结

《Java中Object类的常用方法小结》JavaObject类是所有类的父类,位于java.lang包中,本文为大家整理了一些Object类的常用方法,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. public boolean equals(Object obj)2. public int ha

golang1.23版本之前 Timer Reset方法无法正确使用

《golang1.23版本之前TimerReset方法无法正确使用》在Go1.23之前,使用`time.Reset`函数时需要先调用`Stop`并明确从timer的channel中抽取出东西,以避... 目录golang1.23 之前 Reset ​到底有什么问题golang1.23 之前到底应该如何正确的

C#实现系统信息监控与获取功能

《C#实现系统信息监控与获取功能》在C#开发的众多应用场景中,获取系统信息以及监控用户操作有着广泛的用途,比如在系统性能优化工具中,需要实时读取CPU、GPU资源信息,本文将详细介绍如何使用C#来实现... 目录前言一、C# 监控键盘1. 原理与实现思路2. 代码实现二、读取 CPU、GPU 资源信息1.