- (void)viewDidLoad {
[superviewDidLoad];
//创建登录页面,首先,页面默认加载登录界面
[self createLoginView];
//创建注册页面
//[selfcreateRegistView];
}
//登录按钮调用的方法
- (void)loginBtnClick {
//找到登录页面的两个输入框
UITextField *nameTF = (UITextField*)[_loginView viewWithTag:10086];
UITextField *passwordTF = (UITextField*)[_loginView viewWithTag:10010];
//判断账号密码是否正确
if([nameTF.text isEqualToString:_name] && [passwordTF.text isEqualToString:_password]) {
//如果账号和密码都正确,
NSLog(@"登录成功");
//如果登录成功,把登录视图移除掉
[_loginView removeFromSuperview];
}else {
NSLog(@"账号或密码不正确");
}
}
//注册按钮调用的方法
- (void)registBtnClick {
NSLog(@"注册");
//在显示注册页面之前,把登录页面移除
[_loginView removeFromSuperview];
//把注册页面显示出来
[self.view addSubview:_registView];
//设置透明度,看视图背后有没有东西
// _registView.alpha = 0.5;
}
//创建登录页面视图
- (void)createLoginView {
//创建登录页面视图
_loginView= [[UIView alloc] initWithFrame:self.view.frame];
//把登录页面放到窗口上
[self.view addSubview:_loginView];
//背景图片
UIImageView *backImageView = [[UIImageView alloc] initWithFrame:self.view.frame];
backImageView.image= [UIImage imageNamed:@"1.png"];
[_loginView addSubview:backImageView];
//账号标签
UILabel *nameLable = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 60, 30)];
nameLable.text=@"账号:";
[_loginView addSubview:nameLable];
//密码标签
UILabel *passwordLabel = [[UILabel alloc]initWithFrame:CGRectMake(50, 140, 60, 30)];
passwordLabel.text=@"密码:";
[_loginView addSubview:passwordLabel];
//输入账号
UITextField *nameTF = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 150, 30)];
nameTF.borderStyle=UITextBorderStyleRoundedRect;
[_loginView addSubview:nameTF];
//输入密码
UITextField *passwordTF = [[UITextField alloc] initWithFrame:CGRectMake(100, 140, 150, 30)];
passwordTF.secureTextEntry=YES;
passwordTF.borderStyle=UITextBorderStyleRoundedRect;
[_loginView addSubview:passwordTF];
//设置tag值
nameTF.tag= 10086;
passwordTF.tag= 10010;
//登录按钮
UIButton *loginButton = [UIButton buttonWithType:UIButtonTypeSystem];
loginButton.frame=CGRectMake(80, 200, 40, 30);
[loginButton addTarget:self action:@selector(loginBtnClick) forControlEvents:UIControlEventTouchUpInside];
[loginButton setTitle:@"登录"for State:UIControlStateNormal];
[_loginView addSubview:loginButton];
//注册按钮
UIButton *registButton = [UIButton buttonWithType:UIButtonTypeSystem];
registButton.frame=CGRectMake(200, 200, 40, 30);
[registButton addTarget:self action:@selector(registBtnClick) forControlEvents:UIControlEventTouchUpInside];
[registButton setTitle:@"注册"for State:UIControlStateNormal];
[_loginView addSubview:registButton];
}
//创建注册页面视图
- (void)createRegistView {
//注册页面视图
_registView= [[UIView alloc] initWithFrame:self.view.frame];
//注册页面创建出来先不显示
[self.view addSubview:_registView];
//创建背景图片
UIImageView *backImageView = [[UIImageView alloc] initWithFrame:self.view.frame];
backImageView.image= [UIImageimageNamed:@"2.png"];
[_registView addSubview:backImageView];
//用户名标签
UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 60, 30)];
nameLabel.text=@"用户名:";
[_registView addSubview:nameLabel];
//密码标签
UILabel *passWordLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 140, 60, 30)];
passWordLabel.text=@"密码:";
[_registView addSubview:passWordLabel];
//确认密码标签
UILabel *confirmPasswordLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 180, 80, 30)];
confirmPasswordLabel.text=@"确认密码:";
[_registView addSubview:confirmPasswordLabel];
//输入用户名
UITextField *nameTF = [[UITextField alloc]initWithFrame:CGRectMake(120,100 , 150, 30)];
nameTF.borderStyle=UITextBorderStyleRoundedRect;
[_registView addSubview:nameTF];
//输入密码
UITextField *passwordTF = [[UITextField alloc] initWithFrame:CGRectMake(120,140 , 150, 30)];
passwordTF.borderStyle=UITextBorderStyleRoundedRect;
[_registView addSubview:passwordTF];
//确认密码
UITextField *confirmPasswordTF = [[UITextField alloc] initWithFrame:CGRectMake(120,180 , 150, 30)];
confirmPasswordTF.borderStyle=UITextBorderStyleRoundedRect;
[_registView addSubview:confirmPasswordTF];
//设置三个输入框的tag值
nameTF.tag= 1;
passwordTF.tag= 2;
confirmPasswordTF.tag= 3;
//返回按钮
UIButton*backBtn = [UIButton buttonWithType:UIButtonTypeSystem];
backBtn.frame=CGRectMake(80, 230, 40, 30);
[backBtn setTitleColor:[UIColorredColor] forState:UIControlStateNormal];
[backBtn setTitle:@"返回" forState:UIControlStateNormal];
[backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
[_registView addSubview:backBtn];
//确认注册按钮按钮
UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeSystem];
confirmBtn.frame=CGRectMake(180, 230, 40, 30);
[confirmBtn setTitleColor:[UIColorredColor] forState:UIControlStateNormal];
[confirmBtn setTitle:@"确认" forState:UIControlStateNormal];
[confirmBtn addTarget:self action:@selector(confirm) forControlEvents:UIControlEventTouchUpInside];
[_registView addSubview:confirmBtn];
}
//返回到登录页面
- (void)back {
//先移除注册页面
[_registView removeFromSuperview];
//把登录页面再添加上来
[self.view addSubview:_loginView];
// 找到登录页面的两个输入框
// UITextField *nameTF = (UITextField *)[_loginView viewWithTag:10086];
// UITextField *passwordTF = (UITextField *)[_loginView viewWithTag:10010];
// nameTF.text = _name;
// passwordTF.text = _password;
}
//确认注册
- (void) confirm {
//找到用户名输入框
UITextField *nameTF = (UITextField*)[_registView viewWithTag:1];
//密码输入框
UITextField *passwordTf = (UITextField*)[_registView viewWithTag:2];
//确认密码输入框
UITextField *confirmTF = (UITextField*)[_registView viewWithTag:3];
//判断输入框内容是否为空
if([nameTF.text isEqualToString:@""]) {
NSLog(@"用户名不能为空");
} else if ([passwordTf.text isEqualToString:@""]) {
NSLog(@"密码不能为空");
} else if ([passwordTf.text isEqualToString:confirmTF.text]) {
NSLog(@"注册成功");
//记录注册的信息
_name= nameTF.text;
_password= passwordTf.text;
//自动返回登录页面
[self back];
}else {
NSLog(@"两次输入的密码不相同");
}
}