그래오늘은이거야

objective c UITextField (자동으로 텍스트 이동) (자동으로 텍스트 삭제) auto text append auto text delete 3개 텍스트필드 이동 본문

세상 개발/IOS(Objective-c)

objective c UITextField (자동으로 텍스트 이동) (자동으로 텍스트 삭제) auto text append auto text delete 3개 텍스트필드 이동

jinhongstar 2020. 3. 2. 19:41
728x90
반응형

UITextField

 

shouldChangeCharactersInRange 

textField delegate use to auto add andthen delete

 

 

자동으로 텍스트필드를 이용하여 특정 자리수 체크 및 특정자리수 이상일때 

아래와 같이 옵션을 설정하여 자동으로 숫자가 다음 텍스트 필드로 이동 하거나 이전 텍스트 필드로 이동하게 만들 수 있습니다.

- (BOOL)textField:(UITextField *)inputTextField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    //Auto next step press skill  jinhong.park
    if(inputTextField == firstTextField){
        if (inputTextField.text.length >= 4 && range.length == 0){
//            NSLog(@"4개이상");
            [secondTextField setText:string];
            [self secondTextFieldSelect:nil];
        }
    }else if(inputTextField == secondTextField){
        if (inputTextField.text.length >= 4 && range.length == 0){// over press 4
//                  NSLog(@"4개이상");
                  [thirdTextField setText:string];
                  [self thirdTextFieldSelect:nil];
          }else if(range.location == 0 && range.length == 1 && [string isEqualToString:@""]){ //back button press
                    [secondTextField setText:@""];
                  [self firstTextFieldSelect:nil];
              return NO;

          }
     }else if(inputTextField == thirdTextField){

         if(range.location == 0 && range.length == 1 && [string isEqualToString:@""]){
                       [thirdTextField setText:@""];
                     [self secondTextFieldSelect:nil];
                 return NO;

             }
    }
    
    if (inputTextField.text.length >= 4 && range.length == 0)
        return NO;
    
    return YES; 
}

 

3개의 텍스트 필드 버튼 타입으로 만듭니다.

- (IBAction)firstTextFieldSelect:(id)sender {
    
    CGRect screenBounds = [[UIScreen mainScreen] bounds];
    if (screenBounds.size.height==480) {
        [mainView setContentOffset:CGPointMake(0, 340) animated:YES];
    } else {
        [mainView setContentOffset:CGPointMake(0, 240) animated:YES];
    }
    
    
    if (mainView.frame.origin.y==25) {
        //[self keyboardViewUp];
    }
    
    [firstTextField becomeFirstResponder];
}
반응형
Comments