10

UIViewに複数のテキストフィールドがあります。

textFieldShouldBeginEditingメソッドの前のtextFieldを辞任します。ここでは、次の一連のイベントが実行されます。

  • UIKeyboardWillHideNotificationは、前のフィールドのキーボードが非表示になっているフィールドに対応して受信されます。

  • メソッドtextFieldShouldBeginEditingはYESを返し、その後

  • UIKeyboardWillShowNotificationは、現在のフィールドのキーボードが表示されている場所で受信されます。

ただし、OS 3.2では、textFieldShouldBeginEditingがYESを返しても、現在のフィールドのUIKeyboardWillShowNotificationは受信されません。

ロジックはOS<3.2で機能します

私が間違っているかもしれないアイデアはありますか?

私のコードの一部の下にリストされています(xibには2つのテキストフィールドしかありません)。

私はkeyboardWillShowとkeyboardWillHideで一連の操作を実行する必要がありますOS3.2とOS<3.2でコードを実行する際の違いを見てください

誰かが行動の違いを説明できますか?

.h

@interface ExampleViewController : UIViewController  
{
    IBOutlet UITextField *numericTextField;
    IBOutlet UITextField *alphaTextField;   
    UITextField *lastTextField;
    int lastCursorPos;
    int cursorPosition;
    NSMutableArray *textFields;
}

@property (nonatomic, retain) UITextField *lastTextField;
@property (nonatomic, retain) NSMutableArray *textFields;

@end

.m

- (void)viewWillAppear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) 
                                                 name:UIKeyboardWillShowNotification object:self.view.window]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) 
                                                 name:UIKeyboardWillHideNotification object:self.view.window]; 

    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.textFields = [[NSMutableArray alloc] initWithCapacity:2];
    [self.textFields insertObject:alphaTextField atIndex:0];
    [self.textFields insertObject:numericTextField atIndex:1];
    cursorPosition = 1;
    [numericTextField becomeFirstResponder];
}

-(void)viewWillDisappear:(BOOL)animated 
{
    [self setEditing:NO animated:YES];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; 
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField 
{
    int index;
    for(UITextField *aField in self.textFields){

        if (textField == aField){
            index = [self.textFields indexOfObject:aField];
        }
    }
    if(index>=0 ){
        lastCursorPos = cursorPosition;
        self.lastTextField = [self.textFields objectAtIndex:lastCursorPos-1];
        cursorPosition = index +1;

    }
    [self.lastTextField resignFirstResponder];  

    return YES;
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {        
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES; 
}

- (void)keyboardWillShow:(NSNotification *)notif {
    NSLog(@"Inside keyboardWillShow");
}

- (void)keyboardWillHide:(NSNotification *)notif {      
    NSLog(@"Inside keyboardWillHide");
}
4

3 に答える 3

3

iOS 3.2 以降、2 つのテキスト フィールドを切り替えたときに UIKeyboardWillHideNotification と UIKeyboardWillShowNotification が起動されなくなったと思います。基本的に、通知はキーボードが実際に表示または非表示になっている場合にのみ発生し、あるテキスト フィールドから別のテキスト フィールドに切り替えてもキーボードは非表示にならないため、イベントは発生しません。

iOS 3.2 より前は、フィールドを変更するたびにイベントが発生していました。新しい方法は間違いなくより正しいですが、やろうとしていることは少し難しくなります。

テキスト フィールドのデリゲートを実装したほうがよい場合があります。その後、shouldBeginEditing/didEndEditing イベントを確認できます。または、代わりに、UITextField をサブクラス化し、becomeFirstResponder/resignFirstResponder メソッドをオーバーライドして、それらにフックしてロジックを実装できるようにすることもできます。フィールドはフォーカスを受けたり失ったりします。

于 2011-12-30T01:04:26.630 に答える
1

特定のテキスト フィールドにいるときに、キーボードの種類を変更しようとしていると思います。単純に 2 つの方法を使用してトレースする代わりに、

- (void)textFieldDidBeginEditing:(UITextField *)textField;
- (BOOL)textFieldShouldReturn:(UITextField *)textField;

編集のためにテキストフィールドに触れるたびに、最初のメソッドが呼び出されます。ここにキーボード変更コードを書くことができます

EG: If textfield is of type 1
       set Keyboard Type to alphanumeric.
    Else if textfield is of type 2
       set Keyboard Type to numeric only.

次に、オンスクリーン キーボードの RETURN キーを押すたびに、2 番目のメソッドが呼び出されます。ここで、任意の入力テキスト フィールド コントロールに対して [textfield rejectFirstResponder] ステートメントを記述できます。

これが役立つことを願っています.. :)乾杯!

于 2011-12-02T13:26:56.973 に答える
1

キーボードが表示されると、notificationCenter によってメソッドが呼び出されます。機能しない場合は、オブジェクトを nil に設定します。

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) 
name:UIKeyboardWillShowNotification object:self.view.window]; 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) 
name:UIKeyboardWillHideNotification object:self.view.window];
于 2011-12-20T10:59:23.040 に答える