UITextField的相关操作

本文介绍了一个简单的iOS应用中实现密码登录的功能。通过设置UITextField代理,实现了密码输入验证、实时监听及返回键处理等功能。适用于初学者了解iOS密码登录的基本实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

制作用户密码登录:

设置需要:
//1需要服从<UITextFieldDelegate>
@interface ViewController ()<UITextFieldDelegate>
   只有服从了它才能使用 - (BOOL)textFieldShouldReturn:(UITextField *)textField{
    };
    (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

//2在创建UITextField的时候要声明:_pwdTextField的任务交给self(即自己)去做:
*_pwdTextFiled.delegate = self;* 

//3实时监听TextField:
(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    //将string拼接到原来的内容上 得到即将显示的新的文本
    NSString *newStr = [_pwdTextFiled.text stringByReplacingCharactersInRange:range withString:string];
    }
可对newStr进行操作从而达到操作密码输入的相关操作或者限制
#import "ViewController.h"

@interface ViewController ()<UITextFieldDelegate>
@property (nonatomic, strong) UILabel *alertLabel;
@property (nonatomic, strong) UITextField *pwdTextFiled;
@property (nonatomic, assign) BOOL isLogin;
@property (nonatomic, strong) NSString *firstInputPwdString;
@property (nonatomic, strong) NSString *password;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.password = @"123";

    //设置没有设置过密码
    self.isLogin = NO;

    //创建UILabel
    self.alertLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 60)];
    _alertLabel.textAlignment = NSTextAlignmentCenter;
    _alertLabel.font = [UIFont systemFontOfSize:20];
    [self.view addSubview:_alertLabel];

    //判断显示的内容
    if (_isLogin == NO){
        //设置密码
        self.alertLabel.text = @"请设置密码";
    } else{
        //解锁密码
        self.alertLabel.text = @"请输入解锁密码";
    }

    //创建UITextField
    self.pwdTextFiled = [[UITextField alloc] initWithFrame:CGRectMake(20, 210, self.view.frame.size.width-20-20, 60)];
    _pwdTextFiled.placeholder = @"密码";
    _pwdTextFiled.borderStyle = UITextBorderStyleLine;
    //使文本不可见
    _pwdTextFiled.secureTextEntry = YES;
    _pwdTextFiled.delegate = self;
    [self.view addSubview:_pwdTextFiled];

}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    //弹出键盘
    [self.pwdTextFiled becomeFirstResponder];
}

//响应键盘的return按钮被点击的事件
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self.pwdTextFiled resignFirstResponder];

    //1.确认密码  2.比较密码
    if (_isLogin == YES){
        //登录过了 只需要比较一下和原来的密码是否相同
        if ([_pwdTextFiled.text isEqualToString:_password]){
            //正确
            self.alertLabel.text = @"密码正确 即将跳转";
        } else{
            //不正确
            self.alertLabel.textColor = [UIColor redColor];
            self.alertLabel.text = @"密码不正确 请重新输入";
        }
    } else{
        //设置密码
        //第一次输入密码 第二次输入密码
        if (_firstInputPwdString.length == 0){
            //第一次输入密码
            //保存这一次的密码
            self.firstInputPwdString = self.pwdTextFiled.text;
            //提示请确认密码
            self.alertLabel.text = @"请确认密码";
        } else{
            //第二次确认密码的时候进来
            if ([self.firstInputPwdString isEqualToString:_pwdTextFiled.text]){
                self.alertLabel.text = @"密码设置正确";
                //跳转到主页
            }else{
                //密码不正确
                self.alertLabel.text = @"两次密码不一致 请重新设置";
                _alertLabel.textColor = [UIColor redColor];

                //清空保存第一次的密码
                self.firstInputPwdString = @"";
            }
        }
    }

    self.pwdTextFiled.text = @"";
    return YES;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField{
    self.alertLabel.textColor = [UIColor blackColor];
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    //将string拼接到原来的内容上 得到即将显示的新的文本
    NSString *newStr = [_pwdTextFiled.text stringByReplacingCharactersInRange:range withString:string];

    if (newStr.length == 7) {
        return NO;
    }
    return YES;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值