在开发过程中,某些时我们希望将整个页面放在最上层,这个时候我们就需要手动创建一个UIWindow。
我们创建一个继承自UIWindow的类:
PasswordInputWindow.h 文件
@interfacePasswordInputWindow :UIWindow
{
UIView* view;
UILabel* label;
}
+ (PasswordInputWindow*)shareInstance;
- (void)show;
@end
.........................................................................................................
PasswordInputWindow.m 文件
#import"PasswordInputWindow.h"
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
#define SCREEN_WIDTH[UIScreen mainScreen].bounds.size.width
@implementationPasswordInputWindow
{
UITextField* _textField;
UIWindow* _window;
}
+ (PasswordInputWindow*)shareInstance{
NSLog(@"1");
staticidsharedInstance =nil;
staticdispatch_once_tonceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[selfalloc]initWithFrame:[UIScreenmainScreen].bounds];
});
returnsharedInstance;
}
- (id)initWithFrame:(CGRect)frame{
NSLog(@"3");
self= [superinitWithFrame:frame];
if(self) {
UILabel* labell = [[UILabelalloc]initWithFrame:CGRectMake(10,50,200,20)];
labell.text=@"请输入密码:";
[selfaddSubview:labell];
UITextField* textField = [[UITextFieldalloc]initWithFrame:CGRectMake(10,80,200,20)];
textField.backgroundColor= [UIColorwhiteColor];
[textFieldsetAutocorrectionType:UITextAutocorrectionTypeNo];
[textFieldsetAutocapitalizationType:UITextAutocapitalizationTypeNone];
textField.secureTextEntry=YES;
[selfaddSubview:textField];
UIButton* button= [[UIButtonalloc]initWithFrame:CGRectMake(10,110,200,44)];
[buttonsetBackgroundColor:[UIColorblueColor]];
button.titleLabel.textColor= [UIColorblackColor];
[buttonsetTitle:@"确定"forState:UIControlStateNormal];
[buttonaddTarget:selfaction:@selector(completeButtonPressed:)forControlEvents:UIControlEventTouchUpInside];
[selfaddSubview:button];
self.backgroundColor= [UIColoryellowColor];
_textField= textField;
}
returnself;
}
- (void)show{
[selfmakeKeyWindow];
self.hidden=NO;
}
- (void)completeButtonPressed:(id)sender{
if([_textField.textisEqualToString:@"abcd"]) {
[_textFieldresignFirstResponder];
[selfresignKeyWindow];
self.hidden=YES;
}else{
[selfshowErrorAlertView];
}
}
- (void)showErrorAlertView{
[selfupdataWindows];
}
-(void)updataWindows {
_window= [UIApplicationsharedApplication].keyWindow;
view= [[UIViewalloc]initWithFrame:CGRectMake(40,SCREEN_HEIGHT/2-40,SCREEN_WIDTH-80,80)];
view.backgroundColor= [UIColorblackColor];
view.layer.masksToBounds=YES;
view.layer.cornerRadius=8.0f;
label= [[UILabelalloc]initWithFrame:CGRectMake(0,0,view.bounds.size.width,80)];
label.numberOfLines=3;
label.text=@"密码错误,正确密码是abcd ";
label.textColor= [UIColorwhiteColor];
label.textAlignment=NSTextAlignmentCenter;
[_windowaddSubview:view];
[viewaddSubview:label];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2*NSEC_PER_SEC)),dispatch_get_main_queue(), ^{
//2秒以后移除
[viewremoveFromSuperview];
});
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event{
[selfendEditing:YES];
}
@end
...................................................................................................
ViewController.m文件
#import"ViewController.h"
#import"PasswordInputWindow.h"
@interfaceViewController()
{
UIWindow* _window;
}
@end
@implementationViewController
- (void)viewDidLoad {
[superviewDidLoad];
}
- (IBAction)ClickOnButton:(UIButton*)sender {
[[PasswordInputWindowshareInstance]show];
}
- (void)hideWindow:(UIGestureRecognizer*)gest{
_window.hidden=YES;
_window=nil;
}
@end
第一次发表文章 还请多多指教!