1

我正在使用文本字段输入移动 nuber,我想添加完成按钮来隐藏键盘,以下是我的代码

    - (void)keyboardWillShow:(NSNotification *)note {  
        // create custom button
        UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
        doneButton.frame = CGRectMake(0, 163, 106, 53);
        doneButton.adjustsImageWhenHighlighted = NO;
        [doneButton setImage:[UIImage imageNamed:@"doneup.png"] forState:UIControlStateNormal];
        [doneButton setImage:[UIImage imageNamed:@"donedown.png"] forState:UIControlStateHighlighted];
        [doneButton addTarget:self action:@selector(doneButton:) forControlEvents:UIControlEventTouchUpInside];

        // locate keyboard view
       UIWindow* tempWindow = [[[UIApplication sharedApplication] windows]objectAtIndex:1];
    UIView* keyboard;
    for(int i=0; i<[tempWindow.subviews count]; i++) {
        keyboard = [tempWindow.subviews objectAtIndex:i];
        // keyboard found, add the button
        if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
            if([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES)
                [keyboard addSubview:doneButton];
        } else {
            if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
                [keyboard addSubview:doneButton];
        }
   }
}

但我无法添加完成按钮,在与断点交叉检查后,我观察到控制没有进入 if([[keyboard description] hasPrefix:@"UIKeyboard"] == YES)条件。我正在使用IOS5。

4

1 回答 1

2

我也确实在我的一个项目中添加了一个自定义完成按钮。我使用的教程提到了那段代码:

if([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2)
{
    if([[tmpKeyboard description] hasPrefix:@"<UIPeripheralHost"] == TRUE)
        [tmpKeyboard addSubview:doneButton];
}
else
{
    if([[tmpKeyboard description] hasPrefix:@"<UIKeyboard"] == TRUE)
        [tmpKeyboard addSubview:doneButton];
}

在 iOS 版本 3.2 之前,您使用 UIKeyboard 的方法很好,但后来您必须将其更改为 UIPeripheralHost。

于 2012-05-22T12:02:33.293 回答