//
// ViewController.m
// FicowLoginDemo1
//
// Created by Ficow on 15/11/12.
// Copyright © 2015年 Ficow. All rights reserved.
// #import "ViewController.h" @interface ViewController ()
@property (strong, nonatomic) IBOutlet UITextField *account;
@property (strong, nonatomic) IBOutlet UITextField *password;
@property (strong, nonatomic) IBOutlet UIButton *login;
@property (strong, nonatomic) IBOutlet UIImageView *loginBackground;
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. //Notification监听代码
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(textFieldDidChange)//监听实现的方法
name:@"emptyInput"
object:nil];
NSLog(@"Initiated");
} //实现点击背景隐藏键盘
- (IBAction)backgroundTap:(id)sender{
//将View的class设为UIControl
NSLog(@"ViewTapped");
[[NSNotificationCenter defaultCenter] postNotificationName:@"emptyInput" object:self];
[self.account resignFirstResponder];
[self.password resignFirstResponder];
} //设置键盘的return key为next并跳转到下一个输入框,不要忘了设置return key属性
- (IBAction)nextInput:(id)sender{
NSLog(@"AccountInputFinished");
[[NSNotificationCenter defaultCenter] postNotificationName:@"emptyInput" object:self];
[self.account resignFirstResponder];
[self.password becomeFirstResponder];
} //输入结束,跳转到按钮,并点击按钮
- (IBAction)textFieldDoneEditing:(id)sender{
[[NSNotificationCenter defaultCenter] postNotificationName:@"emptyInput" object:self];
[sender resignFirstResponder];
// [self.login sendActionsForControlEvents:UIControlEventTouchUpInside];
NSLog(@"PasswordInputFinished");
} - (void)textFieldDidChange
{
NSLog(@"Notification is valid");
if (self.account.text.length == || self.password.text.length == ) {
self.login.userInteractionEnabled = NO;
self.login.alpha = 0.5;
self.loginBackground.alpha = 0.2;
}
else{
self.login.userInteractionEnabled = YES;
self.login.alpha = 1.0;
self.loginBackground.alpha = 1.0;
}
} //错误警告弹出框
- (void) errorAlert:(id) sender
:(NSString *) msg
:(NSString *) title{ UIAlertController *controller = [UIAlertController alertControllerWithTitle:title/*警告框的标题*/ message:msg preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确定" /*警告框的确定键*/style:UIAlertActionStyleCancel handler:nil];
[controller addAction:cancelAction];
[self presentViewController:controller animated:YES completion:nil];
} - (IBAction)checkInput:(id)sender{//定义警告视图 NSString *msg = nil;
NSString *title = nil;
if([self.account.text isEqualToString:@"admin"] &&[self.password.text isEqualToString:@""]){
msg = [NSString stringWithFormat:@"登录成功!"];
title = [NSString stringWithFormat:@"Welcome"];
}//demo1进行账号密码判空的部分
else{
if([self.password.text length] == || [self.account.text length] == ){
msg = [NSString stringWithFormat:@"账号/密码不能为空!"];
}
else{
msg = [NSString stringWithFormat:@"账号/密码错误!"];
}
title = [NSString stringWithFormat:@"Error"];
} [self errorAlert:sender
:msg
:title];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end

可以学习一下使用ReactiveCocoa库来做LoginDemo,很好用哦!

04-23 02:06