9

我们如何实现 UIKeyboardTypeNumberPad 以便它有一个“完成”按钮?默认情况下它没有。

4

4 回答 4

13

如果我没有错,那么您想询问如何为 UIKeyboardTypeNumberPad 添加自定义“完成”按钮到键盘。在这种情况下,这可能会有所帮助。在.h 中声明一个 UIButton *doneButton 并将以下代码添加到 .m 文件中

- (void)addButtonToKeyboard {
    // create custom button
    if (doneButton == nil) {
        doneButton  = [[UIButton alloc] initWithFrame:CGRectMake(0, 163, 106, 53)];
    }
    else {
        [doneButton setHidden:NO];
    }

    [doneButton addTarget:self action:@selector(doneButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    // locate keyboard view
    UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
    UIView* keyboard = nil;
    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];
        }
    }
}

- (void)doneButtonClicked:(id)Sender {
//Write your code whatever you want to do on done button tap
//Removing keyboard or something else
}

我在我的应用程序中使用了相同的按钮,因此调整了按钮的框架,因此您可以在需要在键盘上显示完成按钮时调用 [self addButtonToKeyboard ]。否则 UIKeyboardTypeNumberPad 没有完成按钮。在此处输入图像描述

于 2011-01-28T08:45:38.460 回答
2
 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardDidShowNotification object:nil];


//Call this method 

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


   UIButton *doneButton  = [[UIButton alloc] initWithFrame:CGRectMake(0, 163, 106, 53)];
    doneButton.adjustsImageWhenHighlighted = NO;
    [doneButton setImage:[UIImage imageNamed:@"Done.png"] forState:UIControlStateNormal];
   [doneButton addTarget:self action:@selector(doneButton:) forControlEvents:UIControlEventTouchUpInside];
    UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
    UIView* keyboard;
    for(int i=0; i<[tempWindow.subviews count]; i++) {
        keyboard = [tempWindow.subviews objectAtIndex:i];

        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];
        }
    }

}
于 2012-11-27T07:15:52.633 回答
1

iOS 5 问题已解决。太感谢了。

我在显示“完成”按钮时遇到问题。

替换:

 if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
     [keyboard addSubview:doneButton];

在 iOS 5 中没有显示完成按钮...

和:

 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];
 }

工作了一个款待。你是个明星。谢谢,尼古拉 ;-)

于 2011-12-05T17:14:06.667 回答
1

如果有人在尝试在同一个应用程序中加载其他类型的键盘时遇到 DONE 按钮不断弹出的问题,我知道很多应用程序都有这个问题。无论如何,这就是我解决这个问题的方法:在您的 ViewController(您添加 DONE 按钮的同一个视图控制器)中添加此代码(按原样),它应该可以解决您的问题,DONE 按钮不断重新出现。希望它对某些人有所帮助。

- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil];   
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidHideNotification object:nil];

} else {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];  
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

}

于 2012-03-20T13:31:56.480 回答