#import "PINUnlockViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//创建PIN解锁界面
PINUnlockViewController *puvc = [[PINUnlockViewController alloc] init];
//配置windows的启动界面为PIN解锁
self.window.rootViewController = puvc;
return YES;
}
@end
//
// PINUnlockViewController.m
// PrivateAlbum
//
// Created by Pengxiaodong on 15/4/14.
// Copyright (c) 2015年 Pengxiaodong. All rights reserved.
//
#import "PINUnlockViewController.h"
#import "Utilities.h"
#define kPasswordKey @"passwordKey"
@interface PINUnlockViewController ()
@property (nonatomic, strong) UILabel *alertLabel;
@property (nonatomic, strong) UITextField *inputTextField;
@property (nonatomic, strong) UITextField *showingTextField;
@property (nonatomic, strong) NSUserDefaults *userDefaults;
@property (nonatomic, strong) NSString *password;
@property (nonatomic, strong) NSString *tempPassword;
@end
@implementation PINUnlockViewController
- (void)viewDidLoad {
[super viewDidLoad];
//初始化界面
[self initial];
}
- (void)initial{
//读取本地图片
UIImage *bgImage = [UIImage imageNamed:@"Main_BG"];
//设置背景图片
self.view.backgroundColor = [UIColor colorWithPatternImage:bgImage];
//创建导航条视图
[Utilities createImageViewWithFrame:CGRectMake(0, 20, 320, 47) andImageName:@"NavigationBar_BG" superView:self.view];
//创建标题视图
[Utilities createImageViewWithFrame:CGRectMake(100, 34, 120, 19) andImageName:@"NavigationBar_Logo" superView:self.view];
//创建提示的文本框 UILabel
self.alertLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 108, 320, 50)];
_alertLabel.textAlignment = NSTextAlignmentCenter;
_alertLabel.textColor = [UIColor whiteColor];
[self.view addSubview:_alertLabel];
//判断启动之后提示的内容
self.userDefaults = [NSUserDefaults standardUserDefaults];
//获取本地保存的密码
self.password = [_userDefaults objectForKey:kPasswordKey];
if (_password.length == 0) {
//第一次登陆
_alertLabel.text = @"请设置密码";
} else{
_alertLabel.text = @"请输入密码";
}
[Utilities createImageViewWithFrame:CGRectMake(31, 178, 257, 82) andImageName:@"Unlock_PINCode1_Normal" superView:self.view];
self.view.userInteractionEnabled = NO;
//创建用来显示的TextField
self.showingTextField = [[UITextField alloc] initWithFrame:CGRectMake(91, 178, 197, 82)];
_showingTextField.textColor = [UIColor whiteColor];
_showingTextField.font = [UIFont systemFontOfSize:30];
_showingTextField.secureTextEntry = YES;
[self.view addSubview:_showingTextField];
//创建一个用来接受用户输入的TextField
self.inputTextField = [[UITextField alloc] initWithFrame:CGRectZero];
_inputTextField.delegate = self;
[self.view addSubview:_inputTextField];
[_inputTextField becomeFirstResponder];
}
#pragma mark --- UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
/*
textField.text 输入之前显示的内容
range 替换的字符的范围location,lenth (1 ,1)
string 目前输入的字符 或者是@""
*/
//获取当前应该显示的内容
NSString *contentString = [textField.text stringByReplacingCharactersInRange:range withString:string];
//将内容显示
_showingTextField.text = contentString;
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
//判断是不是第一次登陆
if (_password.length == 0) {
if (_tempPassword.length == 0) {
//第一次设置密码
self.tempPassword = textField.text;
_alertLabel.text = @"请确认密码";
}else{
if ([_tempPassword isEqualToString:textField.text]) {
_alertLabel.text = @"密码设置成功";
[_userDefaults setObject:_tempPassword forKey:kPasswordKey];
}else{
_alertLabel.text = @"两次输入不一致 重新设置";
self.tempPassword = @"";
}
}
} else {
//不是第一次登陆,在输入解锁密码
if ([textField.text isEqualToString:_password]){
//输入密码正确
_alertLabel.text = @"密码正确";
} else {
_alertLabel.text = @"密码错误 请重新输入";
}
}
_showingTextField.text = @"";
_inputTextField.text = @"";
return YES;
}
@end
#import "Utilities.h"
@implementation Utilities
//创建背景图片视图
+ (UIImageView *)createImageViewWithFrame:(CGRect)rect andImageName:(NSString *)imageName superView:(UIView *)superView{
UIImageView *tempImageView = [[UIImageView alloc] initWithFrame:rect];
tempImageView.image = [UIImage imageNamed:imageName];
[superView addSubview:tempImageView];
return tempImageView;
}
@end
实现Pinunlock,与滑动解锁相同的部分提取函数到Utilities中
在实现输入输出时,用两个uitextfiled,一个作为show,一个作为input,注意其出现的次序,使用代理
// PINUnlockViewController.h
// PrivateAlbum
#import <UIKit/UIKit.h>
@interface PINUnlockViewController : UIViewController<UITextFieldDelegate>
@end
NavigationController切换页面,两种方法
//
// AppDelegate.m
// NavigationController
//
// Created by 蔡定龙 on 15-4-13.
// Copyright (c) 2015年 李灵杰. All rights reserved.
//
#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
//创建一个导航栏
//为导航栏创建一个界面
ViewController *vc = [[ViewController alloc]init];
//将界面作为导航栏控制器的主界面
UINavigationController *nvc = [[UINavigationController alloc]initWithRootViewController:vc];
//将导航栏控制器设置为window的启动界面
self.window.rootViewController = nvc;
return YES;
}
@end
//
// ViewController.m
// NavigationController
//
// Created by 蔡定龙 on 15-4-13.
// Copyright (c) 2015年 李灵杰. All rights reserved.
//
#import "ViewController.h"
#import "SecondViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor redColor];
UIButton *button =[UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(0, 100, 200, 200);
[button setTitle:@"next" forState:UIControlStateNormal];
[button addTarget:self action:@selector(changetoNext) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)changetoNext{
SecondViewController *svc = [[SecondViewController alloc]init];
[self.navigationController pushViewController:svc animated:YES];
//[self presentViewController:svc animated:YES completion:nil];
}
@end
//
// SecondViewController.m
// NavigationController
//
// Created by 蔡定龙 on 15-4-14.
// Copyright (c) 2015年 李灵杰. All rights reserved.
//
#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(100, 200, 120, 50);
[button setTitle:@"返回" forState:UIControlStateNormal];
[button addTarget:self action:@selector(comeBack) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)comeBack{
//push pop
//[self.navigationController popViewControllerAnimated:YES];
//model dismiss
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
//[self dismissViewControllerAnimated:YES completion:nil];
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end