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 回答