QQ登录页面
#import "ViewController.h"
#import "User.h"
@interface ViewController ()
{
//头像
UIImageView *iconView;
//QQ号码
UITextField *qqTF;
//密码
UITextField *passworldTF;
//密码可见按钮
UIButton *seeBTN;
//登陆按钮
UIButton *loginBTN;
}
@property(nonatomic,strong)NSArray *userArray;
@end
@implementation ViewController
#pragma mark - 重写属性userArray的设置器
-(void)setUserArray:(NSArray *)userArray
{
NSMutableArray *mutableArray=[NSMutableArray array];
for (int i=0; i<userArray.count; i++) {
User *user1=[User userWithDictionary:[userArray objectAtIndex:i]];
[mutableArray addObject:user1];
}
_userArray=mutableArray;
}
#pragma mark - 加载所有的控件
- (void)viewDidLoad {
[super viewDidLoad];
#pragma mark 给属性赋值(user对象)
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"TencentID.plist" ofType:nil];
NSArray *array = [NSArray arrayWithContentsOfFile:plistPath];
self.userArray = array;
#pragma mark 头像
//设置头像外围的红圈
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(120, 50, 80, 80)];
view.layer.cornerRadius = 40;//设置圆角
view.layer.borderWidth= 1.0f;//设置框线宽度
view.layer.borderColor=[[UIColor redColor] CGColor];//设置框线颜色
[self.view addSubview:view];
//设置显示头像的图片框
iconView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 40, 60)];
iconView.image = [UIImage imageNamed:@"1.png"];//设置头像默认显示图片
iconView.center = view.center;
#pragma mark 账号
qqTF = [[UITextField alloc]initWithFrame:CGRectMake(40, 160, 240, 30)];
qqTF.layer.cornerRadius=3;//设置输入框圆角
qqTF.layer.borderWidth= 0.5f;//设置输入框边框宽度
qqTF.layer.borderColor=[[UIColor redColor] CGColor];//设置输入框边框颜色
//设置键盘样式
qqTF.keyboardType=UIKeyboardTypeDecimalPad;
//设置左边label
UILabel *left_qq=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 55, 30)];
left_qq.textAlignment = NSTextAlignmentCenter;//设置文本对齐方式
left_qq.font = [UIFont systemFontOfSize:14];//设置字号
left_qq.backgroundColor=[UIColor redColor];//设置背景颜色
left_qq.textColor=[UIColor whiteColor];//设置字体颜色
left_qq.text = @"账号";//设置显示文本
qqTF.leftViewMode = UITextFieldViewModeAlways;//设置输入框是否显示左边辅助视图
qqTF.leftView = left_qq;//设置输入框左边辅助视图
#pragma mark 密码
passworldTF = [[UITextField alloc]initWithFrame:CGRectMake(40, 205, 240, 30)];
passworldTF.layer.cornerRadius=3;//设置输入框圆角
passworldTF.layer.borderWidth= 0.5f;//设置输入框边框宽度
passworldTF.layer.borderColor=[[UIColor redColor] CGColor];//设置输入框边框颜色
passworldTF.secureTextEntry = YES;//设置输入框文本显示为加密类型
//设置左边label
UILabel *left_pw=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 55, 30)];
left_pw.textAlignment = NSTextAlignmentCenter;//设置文本对齐方式
left_pw.font = [UIFont systemFontOfSize:14];//设置字号
left_pw.backgroundColor=[UIColor redColor];//设置背景颜色
left_pw.textColor=[UIColor whiteColor];//设置字体颜色
left_pw.text = @"密码";//设置显示文本
passworldTF.leftViewMode = UITextFieldViewModeAlways;//设置输入框是否显示右边辅助视图
passworldTF.leftView = left_pw;//设置输入框右边辅助视图
#pragma mark 密码可见按钮
seeBTN = [UIButton buttonWithType:UIButtonTypeCustom];
seeBTN.frame = CGRectMake(195, 237, 90, 30);
[seeBTN setTitle:@"密码可见" forState:UIControlStateNormal];
[seeBTN setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[seeBTN setImage:[UIImage imageNamed:@"cant.png"] forState:UIControlStateNormal];
seeBTN.titleLabel.font = [UIFont systemFontOfSize:13];
[seeBTN addTarget:self action:@selector(canSeePassworldButton:) forControlEvents:UIControlEventTouchUpInside];
#pragma mark 登陆
loginBTN = [UIButton buttonWithType:UIButtonTypeCustom];
loginBTN.frame = CGRectMake(60, 300, 200, 30);
[loginBTN setTitle:@"登 录" forState:UIControlStateNormal];
loginBTN.backgroundColor=[UIColor redColor];
loginBTN.layer.cornerRadius=3;
[loginBTN addTarget:self action:@selector(doLoginButton:) forControlEvents:UIControlEventTouchUpInside];
#pragma mark 添加视图
[self.view addSubview:iconView];
[self.view addSubview:qqTF];
[self.view addSubview:passworldTF];
[self.view addSubview:seeBTN];
[self.view addSubview:loginBTN];
}
#pragma mark - 点击登陆执行的事件
-(void)doLoginButton:(UIButton *)sender
{
//当前两个输入框有一个为空,加载一个提示框(2秒之后消失),函数功能结束
if ([qqTF.text isEqualToString:@""] || [passworldTF.text isEqualToString:@""]) {
[self showAlertviewWithMessage:@"请填写完整的账号信息"];
return;
}
for (User *user in _userArray) {
if ([user.qqID isEqualToString:qqTF.text]) {
if ([user.passworld isEqualToString:passworldTF.text]) {
[self showAlertviewWithMessage:@"登陆成功"];
iconView.image=[UIImage imageNamed:user.icon];
qqTF.text=@"";passworldTF.text=@"";
}
else{
[self showAlertviewWithMessage:@"密码错误"];
}
return;
}
}
[self showAlertviewWithMessage:@"没有这个账号"];
}
#pragma mark - 密码可见按钮执行的事件
-(void)canSeePassworldButton:(UIButton *)sender
{
if (passworldTF.isSecureTextEntry) {
[seeBTN setImage:[UIImage imageNamed:@"can.png"] forState:UIControlStateNormal];
passworldTF.secureTextEntry = NO;
}
else{
[seeBTN setImage:[UIImage imageNamed:@"cant.png"] forState:UIControlStateNormal];
passworldTF.secureTextEntry = YES;
}
}
#pragma mark - 显示提示框
-(void)showAlertviewWithMessage:(NSString *)message
{
/******* 添加提示框 ********/
UIView *black_view = [[UIView alloc]initWithFrame:CGRectMake(60, 500, 200, 30)];
black_view.backgroundColor=[UIColor blackColor];//设置提示框的背景颜色
black_view.layer.cornerRadius = 4;//设置提示框的圆角
black_view.tag=1000;//设置提示框的tag值标记,将用于未来某一时刻删除这个视图
[self.view addSubview:black_view];
/******* 添加提示框上面显示提示信息的文本框 ********/
UILabel *message_label=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200, 30)];
message_label.font=[UIFont systemFontOfSize:15];
message_label.numberOfLines=2;
message_label.textColor=[UIColor whiteColor];
message_label.textAlignment=NSTextAlignmentCenter;
message_label.text = message;
[black_view addSubview:message_label];
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(removeAlertView:) userInfo:nil repeats:NO];
}
#pragma mark - 2秒之后移除提示框事件
-(void)removeAlertView:(NSTimer *)timer
{
[[self.view viewWithTag:1000] removeFromSuperview];
[timer invalidate];//停止计时器
}
@end