聊天(3)---登陆界面

2016-05-06  本文已影响61人  箫声_筱昇

登录界面

屏幕快照 2016-05-06 下午8.40.47.png
#import "LoginViewController.h"
#import "EMSDK.h"
#import "RegisterViewController.h"
#import "UserInfoModel.h"
#import "AppDelegate.h"
#import "MainViewController.h"
@interface LoginViewController ()
@property (nonatomic, strong) UILabel *label_1;//登录
@property (nonatomic, strong) UILabel *label_2;//密码

@property (nonatomic, strong) UITextField *textfield_1;//用户名
@property (nonatomic, strong) UITextField *textfield_2;//密码
@property (nonatomic, strong) UIButton *button_1;
@property (nonatomic, strong) UIButton *button_2;
@property (nonatomic, strong) UIActivityIndicatorView *activityIndicatorView;//风火轮
@end
+ (LoginViewController *)sharedLoginView{
    static LoginViewController *loginVC = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        loginVC = [[LoginViewController alloc] init];
    });
    return loginVC;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    {
        [self.view addSubview:self.label_1];
        [self.view addSubview:self.label_2];
        [self.view addSubview:self.textfield_1];
        [self.view addSubview:self.textfield_2];
        [self.view addSubview:self.button_1];
        [self.view addSubview:self.button_2];
    }
    self.navigationItem.title = @"登录";
}
#pragma mark-------button 的登陆方法的方法回调按钮
- (void)loginAction:(UIButton *)sender{
    BOOL isNotSuccess = [self isSucces];
    
    if (isNotSuccess) {  //说明信息不能为空
        [self.activityIndicatorView startAnimating];
        /**
         *  将登陆操作放到子线程中.比较耗时
         */
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            //登录
            EMError *error = [[EMClient sharedClient] loginWithUsername:self.textfield_1.text password:self.textfield_2.text];
            if (!error) {
                //回到主线程
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.activityIndicatorView stopAnimating];
                    [self.activityIndicatorView removeFromSuperview];
                    [self myAlerMessage:@"登陆成功"];
                    //需要将用户信息保存到本地
                    UserInfoModel  *userInfoModel = [[UserInfoModel alloc] init];
                    userInfoModel.name = self.textfield_1.text;
                    userInfoModel.isLogin = YES;//要将 isLogin 改为 yes;
                    //自动登录
                    [[EMClient sharedClient].options setIsAutoLogin:YES];
                    
                    //跳转到主界面
                    AppDelegate *delegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
                    MainViewController *mainVC = [[MainViewController alloc] init];
                    delegate.window.rootViewController = mainVC;
                    //发送属性列表的广播
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshFriendList" object:nil];
                });
                NSLog(@"登陆成功");
            }else{
                //回到主线程
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.activityIndicatorView stopAnimating];
                    [self.activityIndicatorView removeFromSuperview];
                    [self myAlerMessage:[NSString stringWithFormat:@"登录---失败:%d",error.code]];
                });
            }
        });
    }
}
#pragma mark-------label\textfield\button懒加载
- (UILabel *)label_1{
    if (!_label_1) {
        _label_1 = [[UILabel alloc] initWithFrame:CGRectMake(50, 200, 100, 40)];
        _label_1.text = @"用户";
        _label_1.backgroundColor = [UIColor cyanColor];
    }
    return _label_1;
}

- (UILabel *)label_2{
    if (!_label_2) {
        _label_2 = [[UILabel alloc] initWithFrame:CGRectMake(50, 260, 100, 40)];
        _label_2.text = @"密码";
        _label_2.backgroundColor = [UIColor cyanColor];
    }
    return _label_2;
}

- (UITextField *)textfield_1{
    if (!_textfield_1) {
        _textfield_1 = [[UITextField alloc] initWithFrame:CGRectMake(170, 200, 150, 40)];
        _textfield_1.placeholder = @"请输入用户名";
        _textfield_1.backgroundColor = [UIColor cyanColor];
    }
    return _textfield_1;
}

- (UITextField *)textfield_2{
    if (!_textfield_2) {
        _textfield_2 = [[UITextField alloc] initWithFrame:CGRectMake(170, 260, 150, 40)];
        _textfield_2.placeholder = @"请输入密码";
        _textfield_2.backgroundColor = [UIColor cyanColor];
    }
    return _textfield_2;
}

- (UIButton *)button_1{
    if (!_button_1) {
       _button_1 = [UIButton buttonWithType:UIButtonTypeSystem];
    [_button_1 setFrame:CGRectMake(75,330, 100, 50)];
    [_button_1 setTitle:@"登录" forState: UIControlStateNormal];
    [_button_1 addTarget:self action:@selector(loginAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _button_1;
}
- (UIButton *)button_2{
    if (!_button_2) {
        _button_2 = [UIButton buttonWithType:UIButtonTypeSystem];
        [_button_2 setFrame:CGRectMake(150,330, 100, 50)];
        [_button_2 setTitle:@"注册" forState: UIControlStateNormal];
        [_button_2 addTarget:self action:@selector(RegisterAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _button_2;
}
- (void)RegisterAction:(UIButton *)button{
//进入注册页面
    RegisterViewController *registerVC = [[RegisterViewController alloc] init];
    [self.navigationController pushViewController:registerVC animated:YES];
}
#pragma mark-------在注册过程中,让用户感知到正在注册
- (UIActivityIndicatorView *)activityIndicatorView{
    if (!_activityIndicatorView) {
        _activityIndicatorView = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
        _activityIndicatorView.center = self.view.center;
        _activityIndicatorView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
        _activityIndicatorView.color = [UIColor redColor];
        //直接添加在页面上的话.
        [self.view addSubview:_activityIndicatorView];
        
    }
    return _activityIndicatorView;
}

#pragma mark------提示框;
- (void)myAlerMessage:(NSString*)msg{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"友情提示" message: msg preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style: UIAlertActionStyleCancel handler:nil];
    [alertController addAction:sureAction];
    [self presentViewController:alertController animated:YES completion:nil];
}
#pragma mark------非空判断
- (BOOL)isSucces{
    if (self.textfield_1.text.length == 0) {
        [self myAlerMessage:@"用户名不能为空"];
        return NO;
    }
    if (self.textfield_2.text.length == 0) {
        [self myAlerMessage:@"密码不能为空"];
        return NO;
    }
    return YES;
}

上一篇下一篇

猜你喜欢

热点阅读