iOS LoginDemo
1 // 2 // ViewController.m 3 // FicowLoginDemo1 4 // 5 // Created by Ficow on 15/11/12. 6 // Copyright © 2015年 Ficow. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 11 @interface ViewController () 12 @property (strong, nonatomic) IBOutlet UITextField *account; 13 @property (strong, nonatomic) IBOutlet UITextField *password; 14 @property (strong, nonatomic) IBOutlet UIButton *login; 15 @property (strong, nonatomic) IBOutlet UIImageView *loginBackground; 16 @end 17 18 @implementation ViewController 19 20 - (void)viewDidLoad { 21 [super viewDidLoad]; 22 // Do any additional setup after loading the view, typically from a nib. 23 24 //Notification监听代码 25 [[NSNotificationCenter defaultCenter] addObserver:self 26 selector:@selector(textFieldDidChange)//监听实现的方法 27 name:@"emptyInput" 28 object:nil]; 29 NSLog(@"Initiated"); 30 } 31 32 //实现点击背景隐藏键盘 33 - (IBAction)backgroundTap:(id)sender{ 34 //将View的class设为UIControl 35 NSLog(@"ViewTapped"); 36 [[NSNotificationCenter defaultCenter] postNotificationName:@"emptyInput" object:self]; 37 [self.account resignFirstResponder]; 38 [self.password resignFirstResponder]; 39 } 40 41 //设置键盘的return key为next并跳转到下一个输入框,不要忘了设置return key属性 42 - (IBAction)nextInput:(id)sender{ 43 NSLog(@"AccountInputFinished"); 44 [[NSNotificationCenter defaultCenter] postNotificationName:@"emptyInput" object:self]; 45 [self.account resignFirstResponder]; 46 [self.password becomeFirstResponder]; 47 } 48 49 //输入结束,跳转到按钮,并点击按钮 50 - (IBAction)textFieldDoneEditing:(id)sender{ 51 [[NSNotificationCenter defaultCenter] postNotificationName:@"emptyInput" object:self]; 52 [sender resignFirstResponder]; 53 // [self.login sendActionsForControlEvents:UIControlEventTouchUpInside]; 54 NSLog(@"PasswordInputFinished"); 55 } 56 57 - (void)textFieldDidChange 58 { 59 NSLog(@"Notification is valid"); 60 if (self.account.text.length == 0 || self.password.text.length == 0) { 61 self.login.userInteractionEnabled = NO; 62 self.login.alpha = 0.5; 63 self.loginBackground.alpha = 0.2; 64 } 65 else{ 66 self.login.userInteractionEnabled = YES; 67 self.login.alpha = 1.0; 68 self.loginBackground.alpha = 1.0; 69 } 70 } 71 72 //错误警告弹出框 73 - (void) errorAlert:(id) sender 74 :(NSString *) msg 75 :(NSString *) title{ 76 77 UIAlertController *controller = [UIAlertController alertControllerWithTitle:title/*警告框的标题*/ message:msg preferredStyle:UIAlertControllerStyleAlert]; 78 UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确定" /*警告框的确定键*/style:UIAlertActionStyleCancel handler:nil]; 79 [controller addAction:cancelAction]; 80 [self presentViewController:controller animated:YES completion:nil]; 81 } 82 83 84 - (IBAction)checkInput:(id)sender{//定义警告视图 85 86 NSString *msg = nil; 87 NSString *title = nil; 88 if([self.account.text isEqualToString:@"admin"] &&[self.password.text isEqualToString:@"1234"]){ 89 msg = [NSString stringWithFormat:@"登录成功!"]; 90 title = [NSString stringWithFormat:@"Welcome"]; 91 }//demo1进行账号密码判空的部分 92 else{ 93 if([self.password.text length] == 0 || [self.account.text length] == 0){ 94 msg = [NSString stringWithFormat:@"账号/密码不能为空!"]; 95 } 96 else{ 97 msg = [NSString stringWithFormat:@"账号/密码错误!"]; 98 } 99 title = [NSString stringWithFormat:@"Error"]; 100 } 101 102 [self errorAlert:sender 103 :msg 104 :title]; 105 } 106 107 108 109 110 - (void)didReceiveMemoryWarning { 111 [super didReceiveMemoryWarning]; 112 // Dispose of any resources that can be recreated. 113 } 114 115 @end
可以学习一下使用ReactiveCocoa库来做LoginDemo,很好用哦!
Stay hungry,stay foolish.