iOS入门-14一个简单的登陆页面

概述

为了总结前面的控件现在做一个简单的登陆页面

重点

  • 各个控件的使用
  • 登陆逻辑判断

示例代码

仔细看里面的注释,很好理解

ViewController.h

#import 

@interface ViewController : UIViewController
{
    //声明控件
    UILabel *_lbUserName;
    UILabel *_lbUserPassWord;
    
    UITextField *_tfUserName;
    UITextField *_tfUserPassWord;
    
    UIButton *_btnLogin;
    UIButton *_btnRegister;
}
@end

ViewController.m

#import "ViewController.h"

@interface ViewController () 

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _lbUserName = [UILabel new];
    _lbUserName.frame = CGRectMake(20, 60, 80, 40);
    _lbUserName.text = @"用户名:";
    _lbUserName.font = [UIFont systemFontOfSize:16];
    _lbUserName.textAlignment = NSTextAlignmentLeft;
    
    _lbUserPassWord = [UILabel new];
    _lbUserPassWord.frame = CGRectMake(20, 140, 80, 40);
    _lbUserPassWord.text = @"密码:";
    _lbUserPassWord.font = [UIFont systemFontOfSize:16];
    _lbUserPassWord.textAlignment = NSTextAlignmentLeft;
    
    _tfUserName = [[UITextField alloc] init];
    _tfUserName.frame = CGRectMake(120, 60, 180, 40);
    _tfUserName.placeholder = @"请输入用户名";
    _tfUserName.borderStyle =  UITextBorderStyleRoundedRect;
    _tfUserName.returnKeyType = UIReturnKeyJoin;
    _tfUserName.delegate = self;
    
    _tfUserPassWord = [UITextField new];
    _tfUserPassWord.frame = CGRectMake(120, 140, 180, 40);
    _tfUserPassWord.placeholder = @"请输入密码";
    _tfUserPassWord.secureTextEntry = YES;
    _tfUserPassWord.borderStyle = UITextBorderStyleRoundedRect;
    _tfUserPassWord.returnKeyType = UIReturnKeyJoin;
    _tfUserPassWord.delegate = self;
    
    _btnLogin = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    _btnLogin.frame = CGRectMake(120, 200, 80, 40);
    [_btnLogin setTitle:@"登陆" forState:UIControlStateNormal];
    [_btnLogin addTarget:self action:@selector(pressLogin) forControlEvents:UIControlEventTouchUpInside];
    
    _btnRegister = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    _btnRegister.frame = CGRectMake(180, 200, 80, 40);
    [_btnRegister setTitle:@"注册" forState:UIControlStateNormal];
    [_btnRegister addTarget:self action:@selector(pressRegister) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:_lbUserName];
    [self.view addSubview:_lbUserPassWord];
    [self.view addSubview:_tfUserName];
    [self.view addSubview:_tfUserPassWord];
    [self.view addSubview:_btnLogin];
    [self.view addSubview:_btnRegister];
}

//点击登陆之后的回调
-(void) pressLogin{
    NSLog(@"登陆");
    //做的测试数据
    NSString* strName = @"gong";
    NSString* strPassWord = @"123456";
    
    NSString* strTFName = _tfUserName.text;
    NSString* strTFPassWord = _tfUserPassWord.text;
    
    //下面是对输入内容的逻辑判断
    //模拟成功之后提示一个成功的警示框
    if ([strName isEqualToString:strTFName] && [strPassWord isEqualToString:strTFPassWord]) {
        UIAlertController* controller = [UIAlertController alertControllerWithTitle:@"提示" message:@"登陆成功" preferredStyle:UIAlertControllerStyleAlert];
        
        [controller addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"press sure");
        }]];
        
        [self presentViewController:controller animated:YES completion:nil];
    }else{
        //模拟失败之后提示一个失败的警示框
        UIAlertController* controller = [UIAlertController alertControllerWithTitle:@"提示" message:@"用户名或密码错误" preferredStyle:UIAlertControllerStyleAlert];
        
        [controller addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"press sure");
        }]];
        
        [self presentViewController:controller animated:YES completion:nil];
    }
}

-(void) pressRegister{
    NSLog(@"注册");
}

//点击键盘中右下角search键的回调
-(BOOL) textFieldShouldReturn:(UITextField *)textField{
    NSLog(@"登陆");
    //做的测试数据
    NSString* strName = @"gong";
    NSString* strPassWord = @"123456";
    
    NSString* strTFName = _tfUserName.text;
    NSString* strTFPassWord = _tfUserPassWord.text;
    
    //下面是对输入内容的逻辑判断
    //模拟成功之后提示一个成功的警示框
    if ([strName isEqualToString:strTFName] && [strPassWord isEqualToString:strTFPassWord]) {
        UIAlertController* controller = [UIAlertController alertControllerWithTitle:@"提示" message:@"登陆成功" preferredStyle:UIAlertControllerStyleAlert];
        
        [controller addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        }]];
        
        [self presentViewController:controller animated:YES completion:nil];
        
        return YES;
    }else{
        //模拟失败之后提示一个失败的警示框
        UIAlertController* controller = [UIAlertController alertControllerWithTitle:@"提示" message:@"用户名或密码错误" preferredStyle:UIAlertControllerStyleAlert];
        
        [controller addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        }]];
        
        [self presentViewController:controller animated:YES completion:nil];
        
        return NO;
    }
}

//点击屏幕空白处收起键盘
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [_tfUserName resignFirstResponder];
}

@end

你可能感兴趣的:(iOS)