debug - UITextField 输入完跳入下一field,按钮变化

textField

.h 里创建对象


@interface ***Controller : UIViewController
{
UITextField *phoneNumber;
UITextField *verification;
}

.m 里编写逻辑及其相关的方法

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    if (indexPath.row == 0) {
        phoneNumber = [[UITextField alloc] initWithFrame:CGRectMake(20, 0, 200, 44)];
        phoneNumber.placeholder = @"手机号";
        phoneNumber.font = [UIFont systemFontOfSize:14.0f];
        phoneNumber.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        phoneNumber.keyboardType = UIKeyboardTypePhonePad;

        //

        phoneNumber.returnKeyType = UIReturnKeyDone;
        phoneNumber.delegate = self;

        [cell.contentView addSubview:phoneNumber];
    }
    if (indexPath.row == 1) {
        verification = [[UITextField alloc] initWithFrame:CGRectMake(20, 0, 200, 44)];
        verification.placeholder = @"验证码";
        verification.font = [UIFont systemFontOfSize:14.0f];
        verification.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        [cell.contentView addSubview:verification];

        sendBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        //
        sendBtn.enabled = YES;

        [sendBtn setFrame:CGRectMake(CGRectGetWidth(tableView.frame) - 90, 10, 80, 22)];
        [sendBtn setTitle:@"发送验证码" forState:UIControlStateNormal];
        [sendBtn.titleLabel setFont:[UIFont systemFontOfSize:13.0]];
        [sendBtn setBackgroundColor:COLOR(238, 238, 238, 1)];
        [sendBtn.layer setCornerRadius:10.0f];
        [sendBtn addTarget:self action:@selector(sendButtonClick:) forControlEvents:UIControlEventTouchUpInside];

        [cell.contentView addSubview:sendBtn];
    }

    return cell;
}
        phoneNumber.returnKeyType = UIReturnKeyDone;
        phoneNumber.delegate = self;

        [cell.contentView addSubview:phoneNumber];
        }
    if (indexPath.row == 1) {
        verification = [[UITextField alloc] initWithFrame:CGRectMake(20, 0, 200, 44)];
        verification.placeholder = @"验证码";
        verification.font = [UIFont systemFontOfSize:14.0f];
        verification.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        [cell.contentView addSubview:verification];

        sendBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        //
        sendBtn.enabled = YES;

        [sendBtn setFrame:CGRectMake(CGRectGetWidth(tableView.frame) - 90, 10, 80, 22)];
        [sendBtn setTitle:@"发送验证码" forState:UIControlStateNormal];
        [sendBtn.titleLabel setFont:[UIFont systemFontOfSize:13.0]];
        [sendBtn setBackgroundColor:COLOR(238, 238, 238, 1)];
        [sendBtn.layer setCornerRadius:10.0f];
        [sendBtn addTarget:self action:@selector(sendButtonClick:) forControlEvents:UIControlEventTouchUpInside];

        [cell.contentView addSubview:sendBtn];
    }

    return cell;
}

// 此方法的唯一缺陷是,判断的延迟,当输入完成后,才判断字符串长度
- (BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    // 当输入的数字长度为11时,自动跳入验证码输入框
    if (phoneNumber.text.length == 11) {

       // 验证码框成为第一响应者,即当输入窗口完成时,自动跳到验证码窗口,“发送验证码”按钮亮起  
        [verification becomeFirstResponder];

       // 发送按钮可点击,NO:不可点击
        sendBtn.enabled = YES;

        [sendBtn setBackgroundColor:[UIColor grayColor]];
        return NO;
    } else { [sendBtn setBackgroundColor:COLOR(238, 238, 238, 1)]; }
    return YES;
}

// 在当前框内开始编辑时,触发此方法
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    sendBtn.enabled = YES;
    [sendBtn setBackgroundColor:COLOR(238, 238, 238, 1)];

}

另外,有关UITextField的方法还有- (BOOL)textFieldShouldReturn:(UITextField *)textField

// 该方法 键盘弹出的时候 点return键盘隐藏
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{   
    if ([NameStr isEqualToString:textField.text]) {
        return [NameTF resignFirstResponder];
    }else
    {
        if (textField.text.length > 0 && textField.text.length <= 15) {
            NameStr = NameTF.text;

            [self updateNickName];
        }else
        {
            [Dialog simpleToast:@"请输入昵称并确保不超过20位"];
        }
    }

    return [nickNameTF resignFirstResponder];
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值