没有完成button的UIKeyboardTypeNumberPad

我们如何实现UIKeyboardTypeNumberPad,使其具有“完成”button? 默认情况下它没有。

如果我没有错,那么你想问如何添加一个自定义的“完成”button键盘为UIKeyboardTypeNumberPad。 在这种情况下,这可能会有所帮助。 声明一个UIButton * doneButton in.h并将以下代码添加到.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 } 

我在我的应用程序中使用了相同的function,并调整了button的框架,因此只要您需要在键盘上显示完成button,就可以调用[self addButtonToKeyboard]。 否则,UIKeyboardTypeNumberPad没有完成button。 在这里输入图像说明

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

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

我遇到了显示“完成”button的问题。

取代:

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

它没有显示在iOS 5完成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]; } 

工作了一种享受。 你是个明星。 谢谢,Nicola 😉

如果有人遇到问题,当您尝试在同一个应用程序中加载其他types的键盘时,DONEbutton不断popup,我知道很多应用程序都存在这个问题。 无论如何,这是我如何解决这个问题:在您的ViewController(您添加了DONEbutton相同的视图控制器)添加此代码(按原样),它应该解决您的问题与DONEbutton不断重现。 希望对一些人有帮助。

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

}