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

来源:互联网 发布:iptv机顶盒安装软件 编辑:程序博客网 时间:2024/05/17 23:41

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

    [superviewDidLoad];

    _emailJudge =NO;

    self.view.backgroundColor = [UIColorcolorWithRed:231/255.0green:244/255.0blue:242/255.0alpha:1];

    

    UIButton *back = [UIButtonbuttonWithType:UIButtonTypeCustom];

    back.frame =CGRectMake(15,30, 30,30);

    [back setImage:[UIImageimageNamed:@"登录注册返回.jpg"]forState:UIControlStateNormal];

    [back addTarget:selfaction:@selector(backAction)forControlEvents:UIControlEventTouchUpInside];

    [self.viewaddSubview:back];

    

    _logon = [UIButtonbuttonWithType:UIButtonTypeSystem];

    _logon.frame =CGRectMake(30,370, 315,60);

    _logon.backgroundColor = [UIColorcolorWithRed:203/255.0green:166 /255.0 blue:225 /255.0 alpha:1];

    [_logonsetTitleColor:[UIColorcolorWithRed:118 /255.0 green:135 /255.0 blue:138 /255.0 alpha:1]forState:UIControlStateHighlighted];

    [_logonsetTitleColor:[UIColorblackColor] forState:UIControlStateNormal];

    [_logonsetTitle:@"注册"forState:UIControlStateNormal];

    _logon.userInteractionEnabled =NO;

    _logon.alpha =0.5;

    _logon.layer.cornerRadius =28;

    _logon.layer.masksToBounds =YES;

    [_logonaddTarget:selfaction:@selector(zhuche)forControlEvents:UIControlEventTouchUpInside];

    _logon.titleLabel.font = [UIFont systemFontOfSize:23];

    [self.viewaddSubview:_logon];

    

    _username = [[UITextFieldalloc] initWithFrame:CGRectMake(80, 100, 265,50)];

    _username.placeholder =@"请输入要注册的账号";

       [_usernameaddTarget:selfaction:@selector(textFieldDidChange:)forControlEvents:UIControlEventEditingChanged];

    [self.viewaddSubview:_username];

    _password = [[UITextFieldalloc] initWithFrame:CGRectMake(80, 180, 265,50)];

    _password.placeholder =@"请输入密码";

       [_passwordaddTarget:selfaction:@selector(textFieldDidChange:)forControlEvents:UIControlEventEditingChanged];

    [self.viewaddSubview:_password];

    

    _email = [[UITextFieldalloc] initWithFrame:CGRectMake(80, 260, 265,50)];

    _email.placeholder =@"请输入邮箱(可选)";

    [self.viewaddSubview:_email];

    

    

    UILabel *label = [[UILabelalloc]initWithFrame:CGRectMake(30,100, 48,50)];

    label.text =@"账号";

    label.font = [UIFontsystemFontOfSize:18];

    label.textColor = [UIColorblackColor];

    [self.viewaddSubview:label];

    

    UILabel *label3 = [[UILabelalloc]initWithFrame:CGRectMake(30,180, 48,50)];

    label3.text =@"密码";

    label3.font = [UIFontsystemFontOfSize:18];

    label3.textColor = [UIColorblackColor];

    [self.viewaddSubview:label3];

    

    UILabel *label2 = [[UILabelalloc]initWithFrame:CGRectMake(30,260, 48,50)];

    label2.text =@"邮箱";

    label2.font = [UIFontsystemFontOfSize:18];

    label2.textColor = [UIColorblackColor];

    [self.viewaddSubview:label2];

    

    //

    UIView *line = [[UIViewalloc]initWithFrame:CGRectMake(30,160, 310,1)];

    line.backgroundColor = [UIColorblackColor];

    [self.viewaddSubview:line];

    UIView *line2 = [[UIViewalloc]initWithFrame:CGRectMake(30,240, 310,1)];

    line2.backgroundColor = [UIColorblackColor];

    [self.viewaddSubview:line2];

    

    UIView *line3 = [[UIViewalloc]initWithFrame:CGRectMake(30,320, 310,1)];

    line3.backgroundColor = [UIColorblackColor];

    [self.viewaddSubview:line3];


    

}


- (void)zhuche

{

    AVUser *user = [AVUseruser];

    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 = [[Loginalloc] init];

            [self.navigationControllerpushViewController:dv animated:YES];

            

        } else

        {

           // NSLog(@"%@",error.localizedDescription);

            UIAlertController *alertVC = [UIAlertControlleralertControllerWithTitle:@"注册失败"message:error.localizedDescriptionpreferredStyle:UIAlertControllerStyleAlert];

            [selfpresentViewController:alertVCanimated:YEScompletion:nil];

            UIAlertAction *sureAct1 = [UIAlertActionactionWithTitle:@"确定"style:UIAlertActionStyleCancelhandler:nil];

            [alertVC addAction:sureAct1];

        }

    }];

    

}


- (void)backAction

{

    [self.navigationControllerpopViewControllerAnimated: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


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




0 0