2011-01-17 2 views
2

중국어 (또는 ASCII 문자가 아닌 모든 문자)가 UITextField에 입력되는 것을 방지하려고합니다. 다른 게시물에서 볼 수 있듯이 textField:shouldChangeCharactersInRange:replacementString:을 구현했지만 몇 가지 키를 누른 후에 키보드 위에 나타나는 단어 목록의 중국어 단어를 입력하면 textField:shouldChangeCharactersInRange:replacementString: 메서드가 실행되지 않습니다.textField의 비 ASCII 문자 처리 : shouldChangeCharactersInRange : replacementString :

아이디어가 있으십니까?

답변

2

취할 수있는 해결 방법을 사용하는 것입니다

함수에서 다음
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidChangeNotification object:nil]; 

: 당신이 전체 문자열 매번 교체되기 때문에이 비용이 많이 드는 것을 그러나

- (void)textChanged:(NSNotification *)notification{ 
    //remove observer 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextViewTextDidChangeNotification object:nil]; 

    //change your textfield's value here e.g. 
    myTextField.text = [MyUtils removeNonAsciiChar:myTextField.text]; 

    //add observer again 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidChangeNotification object:nil]; 
} 

참고하지만, 아주 긴 문자열을 기대하지 않는다면 괜찮을 것입니다.

관련 문제