- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; self.window.rootViewController = [self rootViewController]; [self.window makeKeyAndVisible]; return YES; }
@interface PasswordInputView : UIWindow + (PasswordInputView *)shareInstance; - (void)show; @endPasswordInputWindow.m 文件。
#import "PasswordInputView.h" @interface PasswordInputView() @property (nonatomic,weak) UITextField *textField; @end @implementation PasswordInputView #pragma mark - Singleton + (PasswordInputView *)shareInstance { static id instance = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ instance = [[self alloc] initWithFrame:[UIScreen mainScreen].bounds]; }); return instance; } #pragma mark - Initilize - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { [self setup]; } return self; } - (instancetype)initWithCoder:(NSCoder *)decoder { if (self = [super initWithCoder:decoder]) { [self setup]; } return self; } - (void)setup { 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]; self.textField = textField; UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, 110, 200, 44)]; [button setBackgroundColor:[UIColor blueColor]]; [button setTitle:@"确定" forState:UIControlStateNormal]; [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [button addTarget:self action:@selector(completeButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:button]; self.backgroundColor = [UIColor yellowColor]; } #pragma mark - Common Methods - (void)completeButtonPressed:(UIButton *)button { if ([self.textField.text isEqualToString:@"123456"]) { [self.textField resignFirstResponder]; [self resignKeyWindow]; self.hidden = YES; } else { [self showErrorAlertView]; } } - (void)showErrorAlertView { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"密码错误,请重新输入" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; [alertView show]; } - (void)show { [self makeKeyWindow]; self.hidden = NO; } @end
- (void)applicationDidBecomeActive:(UIApplication *)application { [[PasswordInputView shareInstance] show]; }
原文:http://blog.csdn.net/sinat_27706697/article/details/45786153