2012-02-09 17 views

答えて

4

ステップ1で、文字列のあなたの長さを確認してください。プロトコルを実装するクラスを作成しますUITextFieldDelegate

@interface TheDelegateClass : NSObject <UITextFieldDelegate> 

ステップ2:あなたの実装では、メソッドをオーバーライド - (BOOL )textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 
{ 
    // newString is what the user is trying to input. 
    NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string]; 
    if ([newString length] < 1) { 
     // If newString is blank we will just ingore it. 
     return YES; 
    } else 
    { 
     // Otherwise we cut the length of newString to 1 (if needed) and set it to the textField. 
     textField.text = [newString length] > 1 ? [newString substringToIndex:1] : newString; 
     // And make the keyboard disappear. 
     [textField resignFirstResponder]; 
     // Return NO to not change text again as we've already changed it. 
     return NO; 
    } 
} 

手順3:デリゲートクラスのインスタンスをUITextFieldのデリゲートとして設定します。助けのための

TheDelegateClass *theDelegate = [[TheDelegateClass alloc] init]; 
[theTextField setDelegate:theDelegate]; 
+0

提案ありがとうございます。 –

0

私はこれがあると思いコード

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeText:) name:UITextFieldTextDidChangeNotification object:textField]; 

を作成するテキストフィールドに通知を追加し、

- (void) changeText: (id) sender; 
{ 
    if ([textField.text length] == 1) 
    { 
     [textField resignFirstResponder]; 
    }   
} 
+0

おかげで、私はすぐに私が行っボタンを助けを –

0

を実装するには、あなたが探しているしたいですか?

- (BOOL)textFieldShouldReturn:(UITextField *)textField 
{ 
    [textField resignFirstResponder]; 
    [add your method here]; 
    return YES; 

} 

それとも、それはできるだけ早くそれはあなたがtextFieldDidBeginEditingにこのコードを置くことができ、編集し始めとして辞任したい場合:委任方法

[textField resignFirstResponder]; 

チェックこのリンク

https://developer.apple.com/library/ios/#documentation/uikit/reference/UITextFieldDelegate_Protocol/UITextFieldDelegate/UITextFieldDelegate.html

1

テキストデリゲートメソッドでコードを書く必要があります

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 
{ 
    if([textField.text length] == 1){ 
    [textField resignFirstResponder]; 
} 

、その後textFieldDidBeginEditing

- (void)textFieldDidBeginEditing:(UITextField *)textField{ 

    if([textField.text length] == 1){ 
    [textField resignFirstResponder]; 
} 

} 
+0

感謝を押しwihoutテキストフィールドに一つだけの文字を入力するが、私は、すぐに私は一つだけの文字を入力すると、キーボードを返したいとキーボードを返すようにしたいですテキストフィールドでボタンを押したままにする –

+0

shouldChangeCharactersInRangeにコードを書く:(NSRange)range replacementString:(NSString *)stringそれは1文字ずつチェックします – Hiren

0
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 
{ 
    if([textField.text length] == 1){ 
     [textField resignFirstResponder]; 
} 

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField 
{ 
    if([textField.text length]==1) 
    { 
     // here perform the action you want to do 
    } 

} 
関連する問題