2011-10-21 5 views
2

問題が発生した後、数値キーパッドにRETURNボタンを追加することができました。uikeyboardと呼ばれるuitextフィールドを検出する

他のデータを他のuitextフィールドに取り込むときに削除したいのは、キーボードを使用するたびにそのボタンが現れるためです。キーボードの一部です。

KeyBoardDidShowのイベントハンドラを追加するイム、それ以外の場合は仕事をdoesntの、

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardDidShowNotification object:nil]; 

はその後KeyboarWillShow

- (void)keyboardWillShow:(NSNotification *)note { 

    // create custom button 

    NSLog(@"El note es: %@", note); 

    UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    doneButton.frame = CGRectMake(0, 163, 106, 53); 
    doneButton.adjustsImageWhenHighlighted = NO; 
    [doneButton setImage:[UIImage imageNamed:@"done.jpg"] forState:UIControlStateNormal]; 
    [doneButton setImage:[UIImage imageNamed:@"done_pressed.jpg"] forState:UIControlStateHighlighted]; 
    [doneButton addTarget:self action:@selector(doneButton:) forControlEvents:UIControlEventTouchUpInside]; 

    UIWindow *keyboardWindow = nil; 
    for (UIWindow *testWindow in [[UIApplication sharedApplication] windows]) { 
     if (![[testWindow class] isEqual:[UIWindow class]]) { 
      keyboardWindow = testWindow; 
      break; 
     } 
    } 
    if (!keyboardWindow) return; 

    // Locate UIKeyboard. 
    UIView *foundKeyboard = nil; 
    for (UIView *possibleKeyboard in [keyboardWindow subviews]) { 

     // iOS 4 sticks the UIKeyboard inside a UIPeripheralHostView. 
     if ([[possibleKeyboard description] hasPrefix:@"<UIPeripheralHostView"]) { 
      possibleKeyboard = [[possibleKeyboard subviews] objectAtIndex:0]; 
     } 

     if ([[possibleKeyboard description] hasPrefix:@"<UIKeyboard"]) { 
      foundKeyboard = possibleKeyboard; 
      break; 
     } 
    } 

    if (foundKeyboard) { 
     // Add the button to foundKeyboard. 
     [foundKeyboard addSubview:doneButton]; 

    } 
} 

に事は、これは常にボタンを作成することです!明らかに!私は特定のuitextfieldsでボタンを作成したいと思っています。

メソッド - (BOOL)textFieldShouldBeginEditing:最初は実行しませんでした。残りの部分はなぜですか?しかし、もし私がuitextフィールドがキーボードを呼んでいるのを知ることができれば、私の髪は再び成長し、私はよりスリムになるでしょう、誰も助けることができますか?

答えて

0

textFieldShouldBeginEditing:は、テキストフィールドのそれぞれに適切な代理人が設定されている場合(XIBを確認してください)、一貫して起動します。

そして、テキストフィールドにタグ値を設定することができます。タグ1のフィールドの戻りボタンとタグ0のフィールドの戻りボタンを表示することができます。

+0

を[OK]を、私はuitextfieldsためのデリゲートのを修正しました。しかし、問題は、textFieldShouldBeginEditing:が実行されたときにキーボードがロードされないため、キーボードの表示が巧妙ではないので、そこに戻るボタンを追加できないということです。それはkeyboardwillshowからaccesibleですが、私は各uitextfieldの情報にアクセスできません。どのuitextフィールドが最後にタップされたのかを保存する属性を保持する必要がありますか? – subharb

+0

yep ...最後のUITextFieldを保存する属性を保持すると問題ありません。 –

0

キーボード内のサブビューの数をカウントすると、いくつのキーがあるかを知ることができます。それは悪いハックですが、あなたはすでにキーボード:-)

また別の提案を見つけることは悪いハックがあります

if (foundKeyboard) { 
    UIView *doneButton = [foundKeyboard viewWithTag:BUTTON_TAG]; 

    if(numPad){ 
     if(buttonView==nil){ 
      //create doneButton 
      [foundKeyboard addSubview:doneButton];   
     } 
    }else{ 
     [doneButton removeFromSuperview]; 
    } 
} 
関連する問題