2011-01-17 8 views
2

中国語(またはASCII以外のすべての文字)がUITextFieldに入力されないようにしようとしています。他の投稿に見られるように、私はtextField:shouldChangeCharactersInRange:replacementString:を実装しましたが、いくつかのキーを押した後にキーボードの上に表示される単語リストの中から中国語を入力すると、textField:shouldChangeCharactersInRange:replacementString:メソッドは起動しません。textFieldの非アスキー文字の扱い: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]; 
} 

注しかし、あなたは文字列全体を毎回交換するので、これはより高価であることが、あなたは非常に長い文字列を期待していない場合、それは大丈夫でしょう。

関連する問題