UIWindow输入密码

用户从任何一个界面按home键退出,再过一段时间从后台切回来,显示一个输入密码界面,只有用户输入正确的密码,才能进入推出前的界面,我们用一个集成自UIWindow的子类PasswordInputWindow,完成界面显示和逻辑;

@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;
}


-(id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 50, 200, 20)];
        label.text = @"请输入密码";
        label.backgroundColor = [UIColor redColor];
        [self addSubview:label];
        
        
        UITextField * textField = [[UITextField alloc] initWithFrame: CGRectMake(10, 80, 200, 20)];
        textField.backgroundColor = [UIColor whiteColor];
        textField.placeholder =  @"请输入密码";
        textField.secureTextEntry = YES;//密码输入
        textField.clearButtonMode = UITextFieldViewModeAlways; //输入框中是否有个叉号,在什么时候显示,用于一次性删
        textField.clearsOnBeginEditing = YES;  //再次编辑就清空
        [self addSubview:textField];
        
        
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, 110, 200, 44)];
        [button setBackgroundColor:[UIColor blueColor]];
        button.titleLabel.textColor = [UIColor blackColor];
        [button setTitle:@"确定" forState:(UIControlStateNormal)];
        [button addTarget:self action:@selector(completeButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
        
        self.backgroundColor = [UIColor yellowColor];
        _textField = textField;
        
        
        
    }
    
    return self;
}

-(void)show {
    [self makeKeyWindow];//设置成KeyWindow,接受键盘和其他非触摸控件
    self.hidden = NO;
}

-(void)completeButtonPressed:(id)sender {
    if ([_textField.text isEqualToString:@"abcd"]) {
        [_textField resignFirstResponder] ;//键盘消失,取消第一响应者;
        [self resignKeyWindow];//设置成KeyWindow
        self.hidden = YES;
        _textField.text = nil;
    }else {
        
        [self showErrorAlertView];
    }
}


-(void)showErrorAlertView {
   
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"密码错误,正确密码是abcd" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    
    [alertView show];
    
}










@end
只需要在应用进入后台的回调函数中,将该UIWindow显示出来即可
- (void)applicationDidEnterBackground:(UIApplication *)application {
   
    
    [[PasswordInputWindow sharedInstance]show];
    
 
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值