标签:
程序运行显示结果如下 :
验证密码输入错误显示如下:
代码如下 :
1>
//
// PasswordInputWindow.m
// UIWindow--密码框
//
// Created by mac1 on 15/10/5.
// Copyright (c) 2015年 www.iphonetrain.com. All rights reserved.
//
#import "PasswordInputWindow.h"
@interface PasswordInputWindow ()
@property (nonatomic,strong)UITextField *textField;
@end
@implementation PasswordInputWindow
static id instance = nil;
//单例方法创建对象
+ (PasswordInputWindow *)shareInstance
{
//GCD实现单例
static dispatch_once_t onceToken;
dispatch_once(&onceToken,^{
instance =[[self alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
});
return instance;
}
- (id)initWithFrame:(CGRect)frame
{
if (self == [super initWithFrame:frame]) {
//创建Label
UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(10, 50, 200, 20)];
label.text=@"请输入密码";
[self addSubview:label];
//创建输入框
_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 blackColor];
[button setTitle:@"确定" forState:UIControlStateNormal];
[button addTarget:self action:@selector(completeButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:button];
self.backgroundColor=[UIColor greenColor];
}
return self;
}
//显示
- (void)show
{
//使窗口可显示
[self makeKeyAndVisible];
//窗口不隐藏
self.hidden=NO;
}
//密码错误调用的方法
-(void)showErrorAlertView
{
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"输入密码错误" message:@"密码输入错误" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定",nil];
[alertView show];
}
//"确定"按钮触发的方法
- (void)completeButtonPressed:(id)sender{
if ([_textField.text isEqualToString:@"abcd"]) {
//键盘失去第一响应
[_textField resignFirstResponder];
//窗口为第一响应者
[self resignFirstResponder];
self.hidden=YES;
}else{
[self showErrorAlertView];
}
}
@end
2>
外部控制器调用 :
//
// ViewController.m
// UIWindow--密码框
//
// Created by mac1 on 15/10/5.
// Copyright (c) 2015年 www.iphonetrain.com. All rights reserved.
//
#import "ViewController.h"
#import "PasswordInputWindow.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//单例创建对象
PasswordInputWindow *passView = [PasswordInputWindow shareInstance];
//让窗口显示
[passView show];
}
@end
标签:
原文地址:http://www.cnblogs.com/pengsi/p/4855629.html