0

私は次のコードを使用して、UIKeyboard のすぐ上に UIToolbar を追加し、それに接続していました。iPhone OS 4 に切り替えたところ、もう動作していないことに気付きました。

for (UIWindow *keyboardWindow in [[UIApplication sharedApplication] windows]) {
    for (UIView *keyboard in [keyboardWindow subviews]) {

        //print all uiview descriptions

        if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES) {
            .. Some code to attach the UIToolbar
        }   
    }
}

コードがif文に入っていないことに気付きました。if のすぐ上にあるすべての UIView の説明を印刷しようとしましたが、UIKeyboard に関連するものは何も表示されませんでした。コードは OS 3.0 および 3.1 で正常に動作していました。それで、誰かがそれについて何か考えを持っていますか?

4

2 に答える 2

1

以下のコードを使用してみてください。

  - (BOOL) findKeyboard:(UIView *) superView; 
{
    UIView *currentView;
    if ([superView.subviews count] > 0) {
        for(int i = 0; i < [superView.subviews count]; i++)
        {

            currentView = [superView.subviews objectAtIndex:i];
            NSLog(@"%@",[currentView description]);
            if([[currentView description] hasPrefix:@"<UIKeyboard"] == YES)
            {

                NSLog(@"Find it");
                //add toolbar here

                UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, -40, 100, 40)];
                UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Test button" style:UIBarButtonItemStyleBordered target:self action:@selector(buttonClicked:)];
                NSArray *items = [[NSArray alloc] initWithObjects:barButtonItem, nil];
                [toolbar setItems:items];
                [items release];
                [currentView addSubview:toolbar];

                return YES;
            }
            if ([self findKeyboard:currentView]) return YES;
        }
    }

    return NO;

}

-(void) checkKeyBoard {
    UIWindow* tempWindow;

    for(int c = 0; c < [[[UIApplication sharedApplication] windows] count]; c ++)
    {
        tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:c];
        if ([self findKeyboard:tempWindow])
            NSLog(@"Finally, I found it");  
    }
}

- (void)keyboardWillShow:(NSNotification *)note {
    [self performSelector:(@selector(checkKeyBoard)) withObject:nil afterDelay:0];
}

また、AppDelegateの関数didFinishLaunchingWithOptionsにこのコードを追加する必要があります。

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
于 2010-09-13T07:05:04.503 に答える
0

文書化されていない動作に依存しているため、4.0 で動作しなくなっても驚くことではありません。代わりにビューのinputAccessoryViewプロパティを使用してください。これは、OS 3.2+ でこれを行う方法として文書化されています。

于 2010-08-03T21:16:35.550 に答える