您的位置:首页 > 移动开发 > IOS开发

使用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;

    }

写。就可以达到输不输邮箱都能注册成功的目的。

效果图如下



具体代码如下 :

//  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

做法很简单,有更好的做法欢迎大家指教。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios 邮箱 测试 界面 方法