使用LeanCloud注册功能让邮箱在界面可选的方法
2016-09-28 19:41
471 查看
使用LeanCloud做注册功能时,可以只用用户名和密码注册,也可以用户名,密码,邮箱一起注册。如果你在注册方法里加了邮箱的( user.email = _email.text;),那么你不填邮箱是不能成功注册的。如果想把邮箱做成可选可以看下我的方法,写一个BOOL属性,设置初始值为NO,在TextField的监听方法(-(void)textFieldDidChange
:(UITextField *)theTextField)里面,当邮箱输入大于0个字符时,将BOOL属性赋值为YES,再在注册的点击方法里面这样
if (_emailJudge == YES) {
user.email = _email.text;
}
写。就可以达到输不输邮箱都能注册成功的目的。
效果图如下
//
#import "Registered.h"
#import <AVOSCloud/AVOSCloud.h>
#import "Login.h"
@interface Registered ()
@property (nonatomic,
strong) UITextField *username;
@property (nonatomic,
strong) UITextField *password;
@property (nonatomic,
strong) UITextField *email;
@property (nonatomic,
strong) UIButton *logon;
@property (nonatomic,
assign) BOOL emailJudge;
@end
@implementation Registered
- (void)viewDidLoad {
[super
viewDidLoad];
_emailJudge =
NO;
self.view.backgroundColor
= [UIColor
colorWithRed:231/255.0
green:244/255.0
blue:242/255.0
alpha:1];
UIButton *back = [UIButton
buttonWithType:UIButtonTypeCustom];
back.frame =
CGRectMake(15,
30, 30,
30);
[back setImage:[UIImage
imageNamed:@"登录注册返回.jpg"]
forState:UIControlStateNormal];
[back addTarget:self
action:@selector(backAction)
forControlEvents:UIControlEventTouchUpInside];
[self.view
addSubview:back];
_logon = [UIButton
buttonWithType:UIButtonTypeSystem];
_logon.frame =
CGRectMake(30,
370, 315,
60);
_logon.backgroundColor = [UIColor
colorWithRed:203/255.0
green:166 /
255.0 blue:225 /
255.0 alpha:1];
[_logon
setTitleColor:[UIColor
colorWithRed:118 /
255.0
green:135 /
255.0
blue:138 /
255.0
alpha:1]
forState:UIControlStateHighlighted];
[_logon
setTitleColor:[UIColor
blackColor] forState:UIControlStateNormal];
[_logon
setTitle:@"注册"
forState:UIControlStateNormal];
_logon.userInteractionEnabled =
NO;
_logon.alpha =
0.5;
_logon.layer.cornerRadius =
28;
_logon.layer.masksToBounds =
YES;
[_logon
addTarget:self
action:@selector(zhuche)
forControlEvents:UIControlEventTouchUpInside];
_logon.titleLabel.font
= [UIFont
systemFontOfSize:23];
[self.view
addSubview:_logon];
_username = [[UITextField
alloc] initWithFrame:CGRectMake(
80,
100, 265,
50)];
_username.placeholder =
@"请输入要注册的账号";
[_username
addTarget:self
action:@selector(textFieldDidChange:)
forControlEvents:UIControlEventEditingChanged];
[self.view
addSubview:_username];
_password = [[UITextField
alloc] initWithFrame:CGRectMake(
80,
180, 265,
50)];
_password.placeholder =
@"请输入密码";
[_password
addTarget:self
action:@selector(textFieldDidChange:)
forControlEvents:UIControlEventEditingChanged];
[self.view
addSubview:_password];
_email = [[UITextField
alloc] initWithFrame:CGRectMake(
80,
260, 265,
50)];
_email.placeholder =
@"请输入邮箱(可选)";
[self.view
addSubview:_email];
UILabel *label = [[UILabel
alloc]initWithFrame:CGRectMake(30,
100, 48,
50)];
label.text =@"账号";
label.font = [UIFont
systemFontOfSize:18];
label.textColor = [UIColor
blackColor];
[self.view
addSubview:label];
UILabel *label3 = [[UILabel
alloc]initWithFrame:CGRectMake(30,
180, 48,
50)];
label3.text =@"密码";
label3.font = [UIFont
systemFontOfSize:18];
label3.textColor = [UIColor
blackColor];
[self.view
addSubview:label3];
UILabel *label2 = [[UILabel
alloc]initWithFrame:CGRectMake(30,
260, 48,
50)];
label2.text =@"邮箱";
label2.font = [UIFont
systemFontOfSize:18];
label2.textColor = [UIColor
blackColor];
[self.view
addSubview:label2];
//
UIView *line = [[UIView
alloc]initWithFrame:CGRectMake(30,
160, 310,
1)];
line.backgroundColor = [UIColor
blackColor];
[self.view
addSubview:line];
UIView *line2 = [[UIView
alloc]initWithFrame:CGRectMake(30,
240, 310,
1)];
line2.backgroundColor = [UIColor
blackColor];
[self.view
addSubview:line2];
UIView *line3 = [[UIView
alloc]initWithFrame:CGRectMake(30,
320, 310,
1)];
line3.backgroundColor = [UIColor
blackColor];
[self.view
addSubview:line3];
}
- (void)zhuche
{
AVUser *user = [AVUser
user];
user.username =
_username.text;
user.password =
_password.text;
if (_emailJudge ==
YES) {
user.email =
_email.text;
}
[user signUpInBackgroundWithBlock:^(BOOL succeeded,
NSError *error) {
if (succeeded) {
Login *dv = [[Login
alloc] init];
[self.navigationController
pushViewController:dv
animated:YES];
} else
{
// NSLog(@"%@",error.localizedDescription);
UIAlertController *alertVC = [UIAlertController
alertControllerWithTitle:@"注册失败"
message:error.localizedDescription
preferredStyle:UIAlertControllerStyleAlert];
[self
presentViewController:alertVC
animated:YES
completion:nil];
UIAlertAction *sureAct1 = [UIAlertAction
actionWithTitle:@"确定"
style:UIAlertActionStyleCancel
handler:nil];
[alertVC addAction:sureAct1];
}
}];
}
- (void)backAction
{
[self.navigationController
popViewControllerAnimated:YES];
}
-(void)textFieldDidChange :(UITextField *)theTextField
{
if (_username.text.length
> 0 && _password.text.length
> 0 ) {
_logon.userInteractionEnabled =
YES;
_logon.alpha =
1;
}else
{
_logon.userInteractionEnabled =
NO;
_logon.alpha =
0.5;
}
if (_email.text.length
> 0) {
_emailJudge =
YES;
}else
{
_emailJudge =
NO;
}
}
@end
做法很简单,有更好的做法欢迎大家指教。
:(UITextField *)theTextField)里面,当邮箱输入大于0个字符时,将BOOL属性赋值为YES,再在注册的点击方法里面这样
if (_emailJudge == YES) {
user.email = _email.text;
}
写。就可以达到输不输邮箱都能注册成功的目的。
效果图如下
具体代码如下 :
// Copyright © 2016年 RanFeiHong. All rights reserved.//
#import "Registered.h"
#import <AVOSCloud/AVOSCloud.h>
#import "Login.h"
@interface Registered ()
@property (nonatomic,
strong) UITextField *username;
@property (nonatomic,
strong) UITextField *password;
@property (nonatomic,
strong) UITextField *email;
@property (nonatomic,
strong) UIButton *logon;
@property (nonatomic,
assign) BOOL emailJudge;
@end
@implementation Registered
- (void)viewDidLoad {
[super
viewDidLoad];
_emailJudge =
NO;
self.view.backgroundColor
= [UIColor
colorWithRed:231/255.0
green:244/255.0
blue:242/255.0
alpha:1];
UIButton *back = [UIButton
buttonWithType:UIButtonTypeCustom];
back.frame =
CGRectMake(15,
30, 30,
30);
[back setImage:[UIImage
imageNamed:@"登录注册返回.jpg"]
forState:UIControlStateNormal];
[back addTarget:self
action:@selector(backAction)
forControlEvents:UIControlEventTouchUpInside];
[self.view
addSubview:back];
_logon = [UIButton
buttonWithType:UIButtonTypeSystem];
_logon.frame =
CGRectMake(30,
370, 315,
60);
_logon.backgroundColor = [UIColor
colorWithRed:203/255.0
green:166 /
255.0 blue:225 /
255.0 alpha:1];
[_logon
setTitleColor:[UIColor
colorWithRed:118 /
255.0
green:135 /
255.0
blue:138 /
255.0
alpha:1]
forState:UIControlStateHighlighted];
[_logon
setTitleColor:[UIColor
blackColor] forState:UIControlStateNormal];
[_logon
setTitle:@"注册"
forState:UIControlStateNormal];
_logon.userInteractionEnabled =
NO;
_logon.alpha =
0.5;
_logon.layer.cornerRadius =
28;
_logon.layer.masksToBounds =
YES;
[_logon
addTarget:self
action:@selector(zhuche)
forControlEvents:UIControlEventTouchUpInside];
_logon.titleLabel.font
= [UIFont
systemFontOfSize:23];
[self.view
addSubview:_logon];
_username = [[UITextField
alloc] initWithFrame:CGRectMake(
80,
100, 265,
50)];
_username.placeholder =
@"请输入要注册的账号";
[_username
addTarget:self
action:@selector(textFieldDidChange:)
forControlEvents:UIControlEventEditingChanged];
[self.view
addSubview:_username];
_password = [[UITextField
alloc] initWithFrame:CGRectMake(
80,
180, 265,
50)];
_password.placeholder =
@"请输入密码";
[_password
addTarget:self
action:@selector(textFieldDidChange:)
forControlEvents:UIControlEventEditingChanged];
[self.view
addSubview:_password];
_email = [[UITextField
alloc] initWithFrame:CGRectMake(
80,
260, 265,
50)];
_email.placeholder =
@"请输入邮箱(可选)";
[self.view
addSubview:_email];
UILabel *label = [[UILabel
alloc]initWithFrame:CGRectMake(30,
100, 48,
50)];
label.text =@"账号";
label.font = [UIFont
systemFontOfSize:18];
label.textColor = [UIColor
blackColor];
[self.view
addSubview:label];
UILabel *label3 = [[UILabel
alloc]initWithFrame:CGRectMake(30,
180, 48,
50)];
label3.text =@"密码";
label3.font = [UIFont
systemFontOfSize:18];
label3.textColor = [UIColor
blackColor];
[self.view
addSubview:label3];
UILabel *label2 = [[UILabel
alloc]initWithFrame:CGRectMake(30,
260, 48,
50)];
label2.text =@"邮箱";
label2.font = [UIFont
systemFontOfSize:18];
label2.textColor = [UIColor
blackColor];
[self.view
addSubview:label2];
//
UIView *line = [[UIView
alloc]initWithFrame:CGRectMake(30,
160, 310,
1)];
line.backgroundColor = [UIColor
blackColor];
[self.view
addSubview:line];
UIView *line2 = [[UIView
alloc]initWithFrame:CGRectMake(30,
240, 310,
1)];
line2.backgroundColor = [UIColor
blackColor];
[self.view
addSubview:line2];
UIView *line3 = [[UIView
alloc]initWithFrame:CGRectMake(30,
320, 310,
1)];
line3.backgroundColor = [UIColor
blackColor];
[self.view
addSubview:line3];
}
- (void)zhuche
{
AVUser *user = [AVUser
user];
user.username =
_username.text;
user.password =
_password.text;
if (_emailJudge ==
YES) {
user.email =
_email.text;
}
[user signUpInBackgroundWithBlock:^(BOOL succeeded,
NSError *error) {
if (succeeded) {
Login *dv = [[Login
alloc] init];
[self.navigationController
pushViewController:dv
animated:YES];
} else
{
// NSLog(@"%@",error.localizedDescription);
UIAlertController *alertVC = [UIAlertController
alertControllerWithTitle:@"注册失败"
message:error.localizedDescription
preferredStyle:UIAlertControllerStyleAlert];
[self
presentViewController:alertVC
animated:YES
completion:nil];
UIAlertAction *sureAct1 = [UIAlertAction
actionWithTitle:@"确定"
style:UIAlertActionStyleCancel
handler:nil];
[alertVC addAction:sureAct1];
}
}];
}
- (void)backAction
{
[self.navigationController
popViewControllerAnimated:YES];
}
-(void)textFieldDidChange :(UITextField *)theTextField
{
if (_username.text.length
> 0 && _password.text.length
> 0 ) {
_logon.userInteractionEnabled =
YES;
_logon.alpha =
1;
}else
{
_logon.userInteractionEnabled =
NO;
_logon.alpha =
0.5;
}
if (_email.text.length
> 0) {
_emailJudge =
YES;
}else
{
_emailJudge =
NO;
}
}
@end
做法很简单,有更好的做法欢迎大家指教。
相关文章推荐
- Android之使用Http协议实现文件上传功能
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- mysql集群之MMM简单搭建
- 通晓网络测试常用命令
- 主页木马的制作方法
- 解密网页加密的两个方法
- 定义标题的最好方法
- Ruby简明教程之方法(Method)介绍
- Ruby基础知识之方法、代码段
- Ruby面向对象编程中类与方法的基础学习
- C#获取文件夹及文件的大小与占用空间的方法
- Delphi控件ListView的属性及使用方法详解
- VB6实现连接Access数据库的ADODB代码实现方法
- 使用Ajax实时检测"用户名、邮箱等"是否已经存在