OC学习练习——登陆界面实例

一、弹窗

        在旧版本里弹窗是用UIAlertView,但是在现在的版本是用UIAlertController的

        创建好弹窗对象之后,使用alertControllerWithTitle方法为其赋值,传入三个参数

        第一个参数是指弹窗的标题,第二个参数指弹窗显示的信息,第三个参数指弹窗样式

        UIAlertController *alert = [UIAlertController alertControllerWithTitle: @"提示" message: @"验证成功,登陆成功" preferredStyle: UIAlertControllerStyleAlert];

        接下来为弹窗添加按钮,首先用UIAlertAction创建弹窗按钮对象,然后调用actionWithTitle方法赋值,传入两个参数

        第一个参数是指按钮的文本内容,第二个参数是传入一个代码块作为一个执行的操作,如果不需要操作直接传入nil即可

        如果需要多个按钮,直接按这个格式创建多个按钮对象即可

        UIAlertAction *conform = [UIAlertAction actionWithTitle: @"确认" style: UIAlertActionStyleDefault handler: ^(UIAlertAction *_Nonnull action) {

            NSLog(@"点击了确认按钮");

        }];

        创建完弹窗后,调用presentViewController使弹窗显示出来,传入三个参数

        第一个参数指创建好的弹窗对象,第二个参数指是否需要动画效果,第三个参数传入一个代码块,不需要代码块也可以传nil

        [self presentViewController: alert animated: YES completion: nil];

        最后将按钮添加到弹窗中

        [alert addAction: conform];

接下来写登陆界面UI实例

ViewController接口部分:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController {
    UILabel *_bUesrName;
    UITextField *_userName;
    UILabel *_bUserKey;
    UITextField *_uesrKey;
    UIButton *_login;
    UIButton *_register;
}

@property (retain, nonatomic) UILabel *bUserName;
@property (retain, nonatomic) UITextField *userName;
@property (retain, nonatomic) UILabel *bUserKey;
@property (retain, nonatomic) UITextField *userKey;
@property (retain, nonatomic) UIButton *login;
@property (retain, nonatomic) UIButton *register1;

@end

实现部分:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize bUserName = _bUesrName;
@synthesize bUserKey = _bUserKey;
@synthesize userName = _userName;
@synthesize userKey = _userKey;
@synthesize login = _login;
@synthesize register1 = _register;

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor orangeColor];
    
    _bUesrName = [[UILabel alloc] initWithFrame: CGRectMake(50, 100, 300, 70)];
    _bUesrName.text = @"用户名:";
    [self.view addSubview: _bUesrName];
    
    _userName = [[UITextField alloc] initWithFrame: CGRectMake(50, 150, 300, 50)];
    _userName.borderStyle = UITextBorderStyleLine;
    _userName.placeholder = @"请输入用户名......";
    [self.view addSubview: _userName];
    
    _bUserKey = [[UILabel alloc] initWithFrame: CGRectMake(50, 200, 300, 70)];
    _bUserKey.text = @"密码:";
    [self.view addSubview: _bUserKey];
    
    _userKey = [[UITextField alloc] initWithFrame: CGRectMake(50, 250, 300, 50)];
    _userKey.borderStyle = UITextBorderStyleLine;
    _userKey.keyboardType = UIKeyboardTypePhonePad;
    _userKey.placeholder = @"请输入密码......";
    [self.view addSubview: _userKey];
    
    _login = [UIButton buttonWithType: UIButtonTypeRoundedRect];
    _login.frame = CGRectMake(150, 500, 100, 60);
    _login.backgroundColor = [UIColor brownColor];
    [_login setTitle: @"登陆" forState: UIControlStateNormal];
    [_login setTitleColor: [UIColor blueColor] forState: UIControlStateNormal];
    [_login addTarget: self action: @selector(pressLogin) forControlEvents: UIControlEventTouchUpInside];
    [self.view addSubview: _login];
    
   
    _register = [UIButton buttonWithType: UIButtonTypeRoundedRect];
    _register.frame = CGRectMake(150, 600, 100, 60);
    _register.backgroundColor = [UIColor brownColor];
    [_register setTitle: @"注册" forState: UIControlStateNormal];
    [_register setTitleColor: [UIColor blueColor] forState: UIControlStateNormal];
    [_register addTarget: self action: @selector(pressRegister) forControlEvents: UIControlEventTouchUpInside];
    [self.view addSubview: _register];
}

- (void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.userName resignFirstResponder];
    [self.userKey resignFirstResponder];
}

- (void) pressLogin {
    NSString *strName = @"Asina";
    NSString *strKey = @"1234567";
    
    NSString *strTextName = _userName.text;
    NSString *strTextKey = _userKey.text;
    
    if ([strTextName isEqualToString: strName] && [strTextKey isEqualToString: strKey]) {
        NSLog(@"用户名密码正确");
        //显示弹窗
        //在旧版本里弹窗是用UIAlertView,但是在现在的版本是用UIAlertController的
        //创建好弹窗对象之后,使用alertControllerWithTitle方法为其赋值,传入三个参数
        //第一个参数是指弹窗的标题,第二个参数指弹窗显示的信息,第三个参数指弹窗样式
        UIAlertController *alert = [UIAlertController alertControllerWithTitle: @"提示" message: @"验证成功,登陆成功" preferredStyle: UIAlertControllerStyleAlert];
        //接下来为弹窗添加按钮,首先用UIAlertAction创建弹窗按钮对象,然后调用actionWithTitle方法赋值,传入两个参数
        //第一个参数是指按钮的文本内容,第二个参数是传入一个代码块作为一个执行的操作,如果不需要操作直接传入nil即可
        //如果需要多个按钮,直接按这个格式创建多个按钮对象即可
        UIAlertAction *conform = [UIAlertAction actionWithTitle: @"确认" style: UIAlertActionStyleDefault handler: ^(UIAlertAction *_Nonnull action) {
            NSLog(@"点击了确认按钮");
        }];
        //创建完弹窗后,调用presentViewController使弹窗显示出来,传入三个参数
        //第一个参数指创建好的弹窗对象,第二个参数指是否需要动画效果,第三个参数传入一个代码块,不需要代码块也可以传nil
        [self presentViewController: alert animated: YES completion: nil];
        //最后将按钮添加到弹窗中
        [alert addAction: conform];
    } else {
        NSLog(@"用户名密码错误");
        UIAlertController *alert2 = [UIAlertController alertControllerWithTitle: @"提示" message: @"验证失败,登陆失败" preferredStyle: UIAlertControllerStyleAlert];
        UIAlertAction *conform2 = [UIAlertAction actionWithTitle: @"确认" style: UIAlertActionStyleDefault handler: ^(UIAlertAction *_Nonnull action) {
            NSLog(@"点击了确认按钮");
        }];
        [self presentViewController: alert2 animated: YES completion: nil];
        [alert2 addAction: conform2];
    }
}

- (void) pressRegister {
    
}

@end

运行结果:

 正确输入结果:

 

 错误输入结果:

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值