Programming

`textField : shouldChangeCharactersInRange :`를 사용하여 현재 입력 된 문자를 포함한 텍스트를 어떻게 얻습니까?

procodes 2020. 7. 29. 21:23
반응형

`textField : shouldChangeCharactersInRange :`를 사용하여 현재 입력 된 문자를 포함한 텍스트를 어떻게 얻습니까?


아래 코드를 사용 하여 사용자가 입력 할 때마다 textField2텍스트 내용이 업데이트 textField1되도록합니다 textField1.

- (BOOL) textField: (UITextField *)theTextField shouldChangeCharactersInRange: (NSRange)range replacementString: (NSString *)string {    
  if (theTextField == textField1){    
     [textField2 setText:[textField1 text]];    
  }
}

그러나 내가 관찰 한 결과는 ...

textField1이 "123"인 경우 textField2는 "12"입니다.

textField1이 "1234"인 경우 textField2는 "123"입니다.

... 내가 원하는 것은 :

textField1이 "123"인 경우 textField2는 "123"입니다.

textField1이 "1234"인 경우 textField2는 "1234"입니다.

내가 뭘 잘못하고 있죠?


-shouldChangeCharactersInRange텍스트 필드가 실제로 텍스트를 변경 하기 전에 호출 되므로 이전 텍스트 값을 얻습니다. 업데이트 후 텍스트를 얻으려면 다음을 사용하십시오.

[textField2 setText:[textField1.text stringByReplacingCharactersInRange:range withString:string]];

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSString * searchStr = [textField.text stringByReplacingCharactersInRange:range withString:string];

    NSLog(@"%@",searchStr);
    return YES;
}

스위프트 3

허용 된 답변에 따라 Swift 3 에서 다음이 작동해야합니다 .

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

    let newString = NSString(string: textField.text!).replacingCharacters(in: range, with: string)

    return true
}

노트

둘 다 StringNSString라는 방법을 replacingCharacters:inRange:withString. 그러나 예상대로 전자는의 인스턴스를 Range기대하고 후자는의 인스턴스를 기대합니다 NSRange. textField위임 방법은 사용 NSRange인스턴스의 사용, 따라서 NSString이 경우에는.


UITextFieldDelegate를 사용하는 대신 UITextField의 "Editing Changed" 이벤트 를 사용하십시오 .


스위프트 (4)에서 NSString(순수 스위프트) 없이 :

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

    if let textFieldString = textField.text, let swtRange = Range(range, in: textFieldString) {

        let fullString = textFieldString.replacingCharacters(in: swtRange, with: string)

        print("FullString: \(fullString)")
    }

    return true
}

그것에 대한 스위프트 버전 :

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {

    if string == " " {
        return false
    }

    let userEnteredString = textField.text

    var newString = (userEnteredString! as NSString).stringByReplacingCharactersInRange(range, withString: string) as NSString

    print(newString)

    return true
}

이것은 필요한 코드입니다.

if ([textField isEqual:self.textField1])
  textField2.text = [textField1.text stringByReplacingCharactersInRange:range withString:string];

경비를 사용하다

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
        guard case let textFieldString as NSString = textField.text where
            textFieldString.stringByReplacingCharactersInRange(range, withString: string).length <= maxLength else {
                return false
        }
        return true
    }

내 해결책은을 사용하는 것 UITextFieldTextDidChangeNotification입니다.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(copyText:) name:UITextFieldTextDidChangeNotification object:nil];

전화하는 것을 잊지 마세요 [[NSNotificationCenter defaultCenter] removeObserver:self];dealloc방법.


If you need to replace the textfield text with this you can use my solution (Swift 3): https://gist.github.com/Blackjacx/2198d86442ec9b9b05c0801f4e392047

After the replacement you can just get textField.text to retrieve the composed text.

참고URL : https://stackoverflow.com/questions/2198067/using-textfieldshouldchangecharactersinrange-how-do-i-get-the-text-includin

반응형