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

相关文章

Java中的StringUtils.isBlank()方法解读

《Java中的StringUtils.isBlank()方法解读》:本文主要介绍Java中的StringUtils.isBlank()方法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑... 目录所在库及依赖引入方法签名方法功能示例代码代码解释与其他方法的对比总结StringUtils.isBl

使用WPF实现窗口抖动动画效果

《使用WPF实现窗口抖动动画效果》在用户界面设计中,适当的动画反馈可以提升用户体验,尤其是在错误提示、操作失败等场景下,窗口抖动作为一种常见且直观的视觉反馈方式,常用于提醒用户注意当前状态,本文将详细... 目录前言实现思路概述核心代码实现1、 获取目标窗口2、初始化基础位置值3、创建抖动动画4、动画完成后

C#通过进程调用外部应用的实现示例

《C#通过进程调用外部应用的实现示例》本文主要介绍了C#通过进程调用外部应用的实现示例,以WINFORM应用程序为例,在C#应用程序中调用PYTHON程序,具有一定的参考价值,感兴趣的可以了解一下... 目录窗口程序类进程信息类 系统设置类 以WINFORM应用程序为例,在C#应用程序中调用python程序

Java应用如何防止恶意文件上传

《Java应用如何防止恶意文件上传》恶意文件上传可能导致服务器被入侵,数据泄露甚至服务瘫痪,因此我们必须采取全面且有效的防范措施来保护Java应用的安全,下面我们就来看看具体的实现方法吧... 目录恶意文件上传的潜在风险常见的恶意文件上传手段防范恶意文件上传的关键策略严格验证文件类型检查文件内容控制文件存储

CentOS7增加Swap空间的两种方法

《CentOS7增加Swap空间的两种方法》当服务器物理内存不足时,增加Swap空间可以作为虚拟内存使用,帮助系统处理内存压力,本文给大家介绍了CentOS7增加Swap空间的两种方法:创建新的Swa... 目录在Centos 7上增加Swap空间的方法方法一:创建新的Swap文件(推荐)方法二:调整Sww

QT6中绘制UI的两种方法详解与示例代码

《QT6中绘制UI的两种方法详解与示例代码》Qt6提供了两种主要的UI绘制技术:​​QML(QtMeta-ObjectLanguage)​​和​​C++Widgets​​,这两种技术各有优势,适用于不... 目录一、QML 技术详解1.1 QML 简介1.2 QML 的核心概念1.3 QML 示例:简单按钮

Python实现获取带合并单元格的表格数据

《Python实现获取带合并单元格的表格数据》由于在日常运维中经常出现一些合并单元格的表格,如果要获取数据比较麻烦,所以本文我们就来聊聊如何使用Python实现获取带合并单元格的表格数据吧... 由于在日常运维中经常出现一些合并单元格的表格,如果要获取数据比较麻烦,现将将封装成类,并通过调用list_exc

CSS3 布局样式及其应用举例

《CSS3布局样式及其应用举例》CSS3的布局特性为前端开发者提供了无限可能,无论是Flexbox的一维布局还是Grid的二维布局,它们都能够帮助开发者以更清晰、简洁的方式实现复杂的网页布局,本文给... 目录深入探讨 css3 布局样式及其应用引言一、CSS布局的历史与发展1.1 早期布局的局限性1.2

Spring Boot 常用注解整理(最全收藏版)

《SpringBoot常用注解整理(最全收藏版)》本文系统整理了常用的Spring/SpringBoot注解,按照功能分类进行介绍,每个注解都会涵盖其含义、提供来源、应用场景以及代码示例,帮助开发... 目录Spring & Spring Boot 常用注解整理一、Spring Boot 核心注解二、Spr

Oracle 通过 ROWID 批量更新表的方法

《Oracle通过ROWID批量更新表的方法》在Oracle数据库中,使用ROWID进行批量更新是一种高效的更新方法,因为它直接定位到物理行位置,避免了通过索引查找的开销,下面给大家介绍Orac... 目录oracle 通过 ROWID 批量更新表ROWID 基本概念性能优化建议性能UoTrFPH优化建议注