首先需要导入一个第三方的SDK MBProgressHUD ,为了显示提示框,如下图:
在viewController中写控件的属性代码:
@property(nonatomic,strong)UITextField * Account;
@property(nonatomic,strong)UITextField * passWord;
@property(nonatomic,strong)UIButton * Login;
@property(nonatomic,strong)UIButton * Register;
@property(nonatomic,strong)UISwitch * Switch;
利用封装的方法,加载控件的初始化方法
//初始化账号输入框
-(UITextField*)Account
{
if(!_Account)
{
_Account=[[UITextField alloc]initWithFrame:CGRectMake(5, 280, self.view.frame.size.width-10, 50)];
_Account.borderStyle=UITextBorderStyleRoundedRect;
_Account.alpha=0.5;
_Account.placeholder=@"请输入账号";
_Account.textAlignment=NSTextAlignmentCenter;
_Account.keyboardType=UIKeyboardTypeNamePhonePad;
}
return _Account;
}
//初始化密码输入框
-(UITextField* )passWord
{
if(!_passWord)
{
_passWord=[[UITextField alloc]initWithFrame:CGRectMake(5, 350, self.view.frame.size.width-10, 50)];
_passWord.borderStyle=UITextBorderStyleRoundedRect;
_passWord.alpha=0.5;
_passWord.placeholder=@"请输入密码";
_passWord.secureTextEntry=YES;
_passWord.textAlignment=NSTextAlignmentCenter;
_passWord.keyboardType=UIKeyboardTypePhonePad;
}
return _passWord;
}
//初始化登录按钮
-(UIButton* )Login
{
if(!_Login)
{
_Login=[[UIButton alloc]initWithFrame:CGRectMake(20,450,self.view.frame.size.width-40,50)];
_Login.backgroundColor=[UIColor orangeColor];
_Login.alpha=0.8;
_Login.layer.cornerRadius=20;
_Login.layer.masksToBounds=YES;
[_Login setTitle:@"登录" forState:UIControlStateNormal];
[_Login addTarget:self action:@selector(denglu) forControlEvents:UIControlEventTouchUpInside];
}
return _Login;
}
//初始化注册按钮
-(UIButton* )Register
{
if(!_Register)
{
_Register=[[UIButton alloc]initWithFrame:CGRectMake(350,750,self.view.frame.size.width-350,40)];
[_Register setTitle:@"注册" forState:UIControlStateNormal];
[_Register setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[_Register addTarget:self action:@selector(zhuce) forControlEvents:UIControlEventTouchUpInside];
// _Register.backgroundColor=[UIColor redColor];
}
return _Register;
}
//初始化开关
-(UISwitch* )Switch
{
if(!_Switch)
{
_Switch=[[UISwitch alloc]initWithFrame:CGRectMake(5, 750, 100, 40)];
[_Switch addTarget:self action:@selector(changeColor:) forControlEvents:UIControlEventValueChanged];
}
return _Switch;
}
在viewDidLoad中添加控件到主视图:
//添加账号文本框
[self.view addSubview:self.Account];
//添加密码文本框
[self.view addSubview:self.passWord];
//添加登录按钮
[self.view addSubview:self.Login];
//添加注册按钮
[self.view addSubview:self.Register];
//添加开关
[self.view addSubview:self.Switch];
可以设置一些界面的效果:
//导航不透明
self.navigationController.navigationBar.translucent=NO;
self.view.backgroundColor=[UIColor whiteColor];
//标题
self.navigationItem.title=@"登录界面";
//背景视图
UIImageView * imgv=[[UIImageView alloc]initWithFrame:self.view.frame];
imgv.image=[UIImage imageNamed:@"16.jpeg"];
imgv.alpha=0.5;
[self.view addSubview:imgv];
//头像视图
UIImageView * touImgv=[[UIImageView alloc]initWithFrame:CGRectMake((self.view.frame.size.width-100)/2, 100, 100, 100)];
touImgv.layer.cornerRadius=50;
touImgv.layer.masksToBounds=YES;
touImgv.image=[UIImage imageNamed:@"12.jpg"];
[self.view addSubview:touImgv];
写点击和注册的方法:
//实现注册点击方法
-(void)zhuce
{
ZCViewController * zc=[ZCViewController new];
[self.navigationController pushViewController:zc animated:YES];
}
//实现登录点击方法
-(void)denglu
{
//创建NSUserDefaults对象
NSUserDefaults * userDes=[NSUserDefaults standardUserDefaults];
//获取NSUserDefaults的账户
NSString* user = [userDes objectForKey:@"account"];
NSString* pass=[userDes objectForKey:@"password"];
//判断是否成功 如果user,pass和输入框的文字相同,则登录成功,否则登录失败
if([self.Account.text isEqualToString:user] &&[self.passWord.text isEqualToString:pass] )
{
//提示用户登录成功
[self showMBAlertWithMessage:@"登录成功"];
}
else
{
//提示用户登录失败
[self showMBAlertWithMessage:@"账号或密码有误,请重新输入"];
}
}
//实现开关的方法
-(void)changeColor:(UISwitch*)Switch{
if(Switch.isOn){
NSLog(@"开");
}else{
NSLog(@"关");
}
}
//点击屏幕的空白处收起键盘
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event
{
[self.view endEditing:YES];
}
// 设置第三方的显示提示框
-(void)showMBAlertWithMessage:(NSString*)msg {
MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
hud.mode = MBProgressHUDModeText; // 设置文本样式
hud.labelText= msg; // 设置显示的提示文本
hud.removeFromSuperViewOnHide = YES;
[self.view addSubview:hud];
[hud show:YES];
[hud hide:YES afterDelay:3.0];
}
我们上面代码中写到跳转页面到ZCViewController,所以我们需要创建一个继承与UIViewcontroller的视图控制器:
声明属性:
@property(nonatomic,strong)UITextField * Account;
@property(nonatomic,strong)UITextField * passWord;
@property(nonatomic,strong)UIButton * Register;
初始化控件:
//初始化账号输入框
-(UITextField*)Account
{
if(!_Account)
{
_Account=[[UITextField alloc]initWithFrame:CGRectMake(5, 100, self.view.frame.size.width-10, 50)];
_Account.borderStyle=UITextBorderStyleRoundedRect;
_Account.alpha=0.5;
_Account.placeholder=@"请输入账号";
_Account.textAlignment=NSTextAlignmentCenter;
_Account.keyboardType=UIKeyboardTypeNamePhonePad;
}
return _Account;
}
//初始化密码输入框
-(UITextField* )passWord
{
if(!_passWord)
{
_passWord=[[UITextField alloc]initWithFrame:CGRectMake(5, 160, self.view.frame.size.width-10, 50)];
_passWord.borderStyle=UITextBorderStyleRoundedRect;
_passWord.alpha=0.5;
_passWord.placeholder=@"请输入密码";
_passWord.secureTextEntry=YES;
_passWord.textAlignment=NSTextAlignmentCenter;
_passWord.keyboardType=UIKeyboardTypePhonePad;
}
return _passWord;
}
//初始化注册按钮
-(UIButton* )Register
{
if(!_Register)
{
_Register=[[UIButton alloc]initWithFrame:CGRectMake(20,400,self.view.frame.size.width-40,40)];
[_Register setTitle:@"注册" forState:UIControlStateNormal];
[_Register setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[_Register addTarget:self action:@selector(zhuce) forControlEvents:UIControlEventTouchUpInside];
_Register.backgroundColor=[UIColor greenColor];
_Register.layer.cornerRadius=20;
_Register.layer.masksToBounds=YES;
[_Register setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
}
return _Register;
}
在viewDidLoad中写:
//设置页面的背景颜色
self.view.backgroundColor=[UIColor whiteColor];
//设置标题
self.navigationItem.title=@"注册";
//添加账号文本框
[self.view addSubview:self.Account];
//添加密码文本框
[self.view addSubview:self.passWord];
//添加注册按钮
[self.view addSubview:self.Register];
接下里实现点击注册的方法:
//实现注册点击方法
-(void)zhuce
{
//判断是否为空
if(self.Account.text.length!=0 && self.passWord.text.length!=0)
{
//获取输入框的内容
NSString* userStr=self.Account.text;
NSString* passStr=self.passWord.text;
//将获取的文字 存储到NSUserDefaults
NSUserDefaults * userDes=[NSUserDefaults standardUserDefaults];
//存储数据
[userDes setObject:userStr forKey:@"account"];
[userDes setObject:passStr forKey:@"password"];
//提示用户注册成功
[self showMBAlertWithMessage:@"注册成功"];
}
else
{
//提示用户输入内容不能为空
[self showMBAlertWithMessage:@"输入内容不能为空"];
}
}
// 设置第三方的显示提示框
-(void)showMBAlertWithMessage:(NSString*)msg {
MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
hud.mode = MBProgressHUDModeText; // 设置文本样式
hud.labelText= msg; // 设置显示的提示文本
hud.removeFromSuperViewOnHide = YES;
[self.view addSubview:hud];
[hud show:YES];
[hud hide:YES afterDelay:3.0];
}
//点击屏幕的空白处收起键盘
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event
{
[self.view endEditing:YES];
}