numberkeypad上的完成按钮不起作用

时间:2012-05-22 11:46:03

标签: iphone uikeyboard uikeyboardtype

我正在使用文本字段进入移动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。

1 个答案:

答案 0 :(得分: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。