头部加入<UITextFieldDelegate>
并声明全局变量
@property(nonatomic,strong)UITextField *textField;
@property(nonatomic,strong)NSMutableArray *tfArray;
-(void)initUI{
_tfArray = [[NSMutableArray alloc] initWithArray:@[[UITextField new],[UITextField new],[UITextField new],[UITextField new],[UITextField new],[UITextField new]]];
[_tfArray enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
UITextField *textField = (UITextField *)obj;
textField.backgroundColor = [UIColor clearColor];
textField.tintColor = [UIColor clearColor];
textField.enabled = NO;
textField.font = Font_20;
textField.textAlignment = NSTextAlignmentCenter;
textField.tag = idx;
[self.view addSubview:textField];
}];
[_tfArray mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:15 leadSpacing:55 tailSpacing:55];
[_tfArray mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(noticeLab.mas_bottom).mas_offset(30);
make.height.mas_equalTo(60);
}];
_textField = [UITextField new];
_textField.delegate = self;
[_textField addTarget:self action:@selector(textChange:) forControlEvents:UIControlEventEditingChanged];
_textField.backgroundColor = [UIColor clearColor];
_textField.textColor = [UIColor clearColor];
_textField.tintColor = [UIColor clearColor];
_textField.keyboardType = UIKeyboardTypeNumberPad;
[self.view addSubview:_textField];
[_textField mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.view).offset(55);
make.right.equalTo(self.view).offset(-55);
make.top.equalTo(_tfArray[0]);
make.bottom.equalTo(_tfArray[0]);
}];
}
-(void)textChange:(UITextField *)textField{
if (textField.text.length > _tfArray.count) {
_textField.text = [_textField.text substringWithRange:NSMakeRange(0,_tfArray.count)];
}
else
{
NSString *subStr = [textField.text substringWithRange:NSMakeRange(textField.text.length-1, 1)];
if(textField.text.length >0)
{
UITextField *boxTextField = [_tfArray objectAtIndex:(textField.text.length-1)];
boxTextField.text = subStr;
if (boxTextField == _tfArray.lastObject) {
[self checkSms:_textField.text];
}
}
}
}
- (void)textFieldDidDeleteBackward:(UITextField *)textField {
NSLog(@"%@",textField.text);
UITextField *boxTextField = [_tfArray objectAtIndex:(textField.text.length)];
boxTextField.text = @"";
}
网友评论