手工创建UIWindow

手工创建UIWindow,可以在应用开发中将某些界面覆盖到最上层

/* eg.支付宝客户端的手势解锁功能\ 密码保护输入界面\ 应用的启动介绍页\ 应用内的通知提醒显示\ 应用内的弹框广告等

// 如果我们创建的 UIWindow 需要处理键盘事件,那就需要将其合理的设置为 keyWindow. keyWindow是被系统设计用来接收键盘和其他非触摸事件的 UIWindow.我们可以通过makeKeyWindow 和 resignKeyWindow来将自己创建的UIWindow 设置成keyWindow.但不要滥用


示例代码: 实现一个继承自 UIWindow 的子类 PasswordInputWindow ,完成密码输入界面的显示和处理逻辑..

// 只需应用进入后台的回调函数调用即可

- (void)applicationDidEnterBackground:(UIApplication *)application {

     [[PasswordInputWindow sharedInstance] show];

}



#import<UIKit/UIKit.h>

@interface PasswordInputWindow : UIWindow

+ (PasswordInputWindow *)sharedInstance;

- (void)show;

@end


#import "PasswordInputWindow.h"

@implementation PasswordInputWindow

{

UITextField * _textField;

}

+ (PasswordInputWindow *)sharedInstance{

static id sharedInstance = nil;

static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{

sharedInstance = [[self alloc] initWithFrame:[UIScreen mainScreen].bounds];

});

return sharedInstance;

}

- (void)show{

[self makeKeyWindow];// 如果我们创建的 UIWindow 需要处理键盘事件,那就需要将其合理的设置为 keyWindow. keyWindow是被系统设计用来接收键盘和其他非触摸事件的 UIWindow.我们可以通过makeKeyWindow 和 resignKeyWindow来将自己创建的UIWindow 设置成keyWindow.但不要滥用

self.hidden = NO;

}

- (instancetype)initWithFrame:(CGRect)frame{

if (self = [super initWithFrame:frame]) {

UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(10, 50, 200, 20)];

label.text = @"清输入密码";

[self addSubview:label];

UITextField * textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 80, 200, 20)];

textField.backgroundColor = [UIColor whiteColor];

textField.secureTextEntry = YES;

[self addSubview:textField];

UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(10, 110, 200, 44)];

[button setBackgroundColor:[UIColor blueColor]];

button.titleLabel.textColor = [UIColor whiteColor];

[button setTitle:@"确定" forState: UIControlStateNormal];

[button addTarget:self action:@selector(completeButtonClicked:) forControlEvents:UIControlEventTouchUpInside];

[self addSubview:button];

self.backgroundColor = [UIColor lightGrayColor];

_textField = textField;

}

return self;

}

- (void)completeButtonClicked:(id)sender{

if ([_textField.text isEqualToString:@"abcd"]) {

[_textField resignFirstResponder];

[self resignKeyWindow];

self.hidden = YES;

}else{

[self showErrorAlertView];

}

}

- (void)showErrorAlertView{

UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:nil message:@"密码错误" delegate: self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

[alertView show];

}

@end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 在开发过程中,某些时我们希望将整个页面放在最上层,这个时候我们就需要手动创建一个UIWindow。 我们创建一个继...
    十八掌阅读 5,928评论 0 0
  • *7月8日上午 N:Block :跟一个函数块差不多,会对里面所有的内容的引用计数+1,想要解决就用__block...
    炙冰阅读 7,346评论 1 14
  • //设置尺寸为屏幕尺寸的时候self.window = [[UIWindow alloc] initWithFra...
    LuckTime阅读 4,298评论 0 0
  • iOS开发系列--网络开发 概览 大部分应用程序都或多或少会牵扯到网络开发,例如说新浪微博、微信等,这些应用本身可...
    lichengjin阅读 9,191评论 2 7
  • 哦吼吼,又研究了几天,把FMDB这个封装好的数据库搞定了,写了个简单的例子,基于FMDB的添删改查操作,界面很一般...
    lichengjin阅读 3,647评论 0 0