• UI案例——登陆系统


    UI的登陆界面实例

    在学习了UILabel,UIButton,UIView,UITextField的内容之后,我们就可以写一个简单的登陆界面

    我们可以通过UILabel来编写我们显示在登陆界面上的文字比方说下面这两行字就是通过UILabel去实现的。

    在这里插入图片描述

    下面给出一下实现的代码

    _lbUserName = [[UILabel alloc] init];
        _lbUserName.frame = CGRectMake(20, 60, 80, 40);//设置位置和大小
        _lbUserName.text = @"用户名";
        _lbUserName.font = [UIFont systemFontOfSize:20];//设置字体大小
        _lbUserName.textAlignment = NSTextAlignmentLeft;
        _lbUserPassword = [[UILabel alloc] initWithFrame:CGRectMake(20, 140, 80, 40)];
        _lbUserPassword.text = @"密码";
        _lbUserPassword.font = [UIFont systemFontOfSize:20];
        _lbUserPassword.textAlignment = NSTextAlignmentLeft;
    

    为了给用户返回提示,设计了一个警告对话框来用于给用户返回提示,这个警告对话框是通过UIAlertController来实现的,下面给出代码

    在这里插入图片描述

    				UIAlertController* alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"用户名和密码正确" preferredStyle:UIAlertControllerStyleAlert];//p1:标题名字,p2:显示的信息,p3是用来指定 UIAlertController 的样式的。UIAlertControllerStyleAlert 是其中一种可选的样式。
            UIAlertAction* confirm = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction* _Nonnull action) {
                NSLog(@"点击确认");
            }];//UIAlertAction 是用来创建 UIAlertController 中的按钮的。p1:按钮标题,p2:风格,p3:按钮的点击事件处理
            [alertController addAction:confirm];//给这个弹窗添加上确认这个按钮
            UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction* _Nonnull action) {
                NSLog(@"点击取消");
            }];
            [alertController addAction: cancel];
            [self presentViewController:alertController animated:YES completion:nil];
    

    创建登陆和注册两个按钮,来实现点击登陆的效果

    在这里插入图片描述

    		_btLogin = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        _btLogin.frame = CGRectMake(120, 300, 80, 40);
        [_btLogin setTitle:@"登陆" forState:UIControlStateNormal];//设置按钮类型
        [_btLogin addTarget:self action:@selector(pressLogin) forControlEvents:UIControlEventTouchUpInside];//为按钮添加点击事件
        [self.view addSubview:_btLogin];
    

    下面给出一下整体实现的代码

    #import "ViewController.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.view.backgroundColor = [UIColor orangeColor];
        _lbUserName = [[UILabel alloc] init];
        _lbUserName.frame = CGRectMake(20, 60, 80, 40);
        _lbUserName.text = @"用户名";
        _lbUserName.font = [UIFont systemFontOfSize:20];
        _lbUserName.textAlignment = NSTextAlignmentLeft;
        _lbUserPassword = [[UILabel alloc] initWithFrame:CGRectMake(20, 140, 80, 40)];
        _lbUserPassword.text = @"密码";
        _lbUserPassword.font = [UIFont systemFontOfSize:20];
        _lbUserPassword.textAlignment = NSTextAlignmentLeft;
        [self.view addSubview:_lbUserName];
        [self.view addSubview:_lbUserPassword];
        
        _tfUserName = [[UITextField alloc] initWithFrame:CGRectMake(120, 60, 150, 40)];
        _tfUserName.placeholder = @"请输入用户名";
        _tfUserName.borderStyle = UITextBorderStyleRoundedRect;
        _tfUserPassword = [[UITextField alloc] initWithFrame:CGRectMake(120, 140, 150, 40)];
        _tfUserPassword.placeholder = @"请输入密码";
        _tfUserPassword.borderStyle = UITextBorderStyleRoundedRect;
        _tfUserPassword.secureTextEntry = YES;
        [self.view addSubview:_tfUserName];
        [self.view addSubview:_tfUserPassword];
        
        _btLogin = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        _btLogin.frame = CGRectMake(120, 300, 80, 40);
        [_btLogin setTitle:@"登陆" forState:UIControlStateNormal];
        [_btLogin addTarget:self action:@selector(pressLogin) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_btLogin];
        
        _btRegister = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        _btRegister.frame = CGRectMake(120, 360, 80, 40);
        [_btRegister setTitle:@"注册" forState:UIControlStateNormal];
        [_btRegister addTarget:self action:@selector(pressRegister) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_btRegister];
        // Do any additional setup after loading the view.
    }
    - (void) pressLogin {
        NSString* strName = @"michael";
        NSString* strPass = @"12345678";
        if ([strPass isEqual: _tfUserPassword.text] && [strName isEqual: self.tfUserName.text]) {
            NSLog(@"用户名密码正确");
            UIAlertController* alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"用户名和密码正确" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction* confirm = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction* _Nonnull action) {
                NSLog(@"点击确认");
            }];
            [alertController addAction:confirm];
            UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction* _Nonnull action) {
                NSLog(@"点击取消");
            }];
            [alertController addAction: cancel];
            [self presentViewController:alertController animated:YES completion:nil];
        } else {
            NSLog(@"用户名或密码错误");
            UIAlertController* alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"用户名或密码错误" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction* confirm = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction* _Nonnull action) {
                NSLog(@"点击确认");
            }];
            [alertController addAction:confirm];
            UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction* _Nonnull action) {
                NSLog(@"点击取消");
            }];
            [alertController addAction: cancel];
            [self presentViewController:alertController animated:YES completion:nil];
        }
    }
    - (void) pressRegister {
        NSLog(@"ddd");
    }
    
    -(void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
        [_tfUserName resignFirstResponder];
        [_tfUserName resignFirstResponder];
    }
    @end
    
    

    最后实现的效果:

    在这里插入图片描述

    登陆成功的界面:

    在这里插入图片描述

  • 相关阅读:
    GitLab使用记录
    数字化门店| 奶茶店智慧管理系统
    springboot+jsp教师工作量业务数据统计与分析系统ssm高校教师电子业务档案管理系统
    Linux下如何切换多版本Python
    jsch在虚拟机环境下连接的坑
    3、核心配置文件
    13.django-admin组件
    谈谈制定数据治理战略路线图的方法
    .net core中前端vue HTML5 History 刷新页面404问题
    vmware centos7虚拟机出现 Generating /run/initramfs/rdsosreport.txt 问题?
  • 原文地址:https://blog.csdn.net/qq_73106050/article/details/139390582