使用LeanCloud注册功能让邮箱在界面可选的方法

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


做法很简单,有更好的做法欢迎大家指教。




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值