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

来源:互联网 发布:上海公交车软件 编辑:程序博客网 时间:2024/06/09 19:09

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
原创粉丝点击