您的位置:首页 > 其它

LoginDemo

2015-11-20 17:28 190 查看
//
//  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 == 0 || self.password.text.length == 0) {
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:@"1234"]){
msg = [NSString stringWithFormat:@"登录成功!"];
title = [NSString stringWithFormat:@"Welcome"];
}//demo1进行账号密码判空的部分
else{
if([self.password.text length] == 0 || [self.account.text length] == 0){
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: