试图将完成button添加到数字键盘

我试图添加一个“完成”button到UIKeyboadnumpad,但没有成功。 我的代码有什么问题?

键盘没有完成button

@implementation DemoViewController - (void)loadView { self.view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame]; self.view.backgroundColor = [UIColor groupTableViewBackgroundColor]; textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 200, 300, 26)]; textField.borderStyle = UITextBorderStyleRoundedRect; textField.keyboardType = UIKeyboardTypeNumberPad; textField.returnKeyType = UIReturnKeyDone; textField.textAlignment = UITextAlignmentLeft; textField.text = @"12345"; [self.view addSubview:textField]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; } - (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:0]; UIView* keyboard; for(int i=0; i<[tempWindow.subviews count]; i++) { keyboard = [tempWindow.subviews objectAtIndex:i]; // keyboard view found; add the custom button to it 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)doneButton:(id)sender { NSLog(@"Input: %@", textField.text); [textField resignFirstResponder]; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationPortrait); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; [textField release]; [super dealloc]; } @end 

解决scheme 如果屏幕上还有其他非数字键盘文本字段,则是完美的。

inputAccessoryView

 - (void)viewDidLoad { [super viewDidLoad]; UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)]; numberToolbar.barStyle = UIBarStyleBlackTranslucent; numberToolbar.items = [NSArray arrayWithObjects: [[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelNumberPad)], [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], [[UIBarButtonItem alloc]initWithTitle:@"Apply" style:UIBarButtonItemStyleDone target:self action:@selector(doneWithNumberPad)], nil]; [numberToolbar sizeToFit]; numberTextField.inputAccessoryView = numberToolbar; } -(void)cancelNumberPad{ [numberTextField resignFirstResponder]; numberTextField.text = @""; } -(void)doneWithNumberPad{ NSString *numberFromTheKeyboard = numberTextField.text; [numberTextField resignFirstResponder]; } 

我需要电话本(+ +#)而不是数字键盘,我甚至没有在angular落里的空button。

写你的添加button的代码- (void)keyboardDidShow:(NSNotification *)note

代替

 - (void)keyboardWillShow:(NSNotification *)note 

对于这个实现UIKeyboardDidShowNotification通知如:

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil]; 

我觉得UIView* keyboard; 没有得到键盘的视图,因为键盘还没有显示,它会显示!

一些教程是不完整的,或者更老。 本教程从IOS 4.3起,我检查了它。 保存这两个图像graphics,并粘贴到代码中。 有很less改变。 链接在这里。

PS。 我不以任何方式与本文关联,但发现它是完整的。

http://www.neoos.ch/blog/37-uikeyboardtypenumberpad-and-the-missing-return-key

为了简化:截图,剪下整个退格键,水平翻转,在Photoshop中清除其退格符号,并用我们想要的文本覆盖返回键。 我们select将其标记为完成
现在我们有我们自定义button的UIControlStateNormal的图像。
重复整个过程(在截图时使用触摸的退格键)来获取我们button的UIControlStateHighlighted的第二个图像。

结果如下:< missing image >


现在回到编码:
首先,我们需要知道数字键盘何时会在屏幕上向上滑动,以便在发生这种情况之前插入我们的自定义button。
幸运的是,有一个确切的通知,并注册它是一样简单:

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; 

一旦完成整个事情,不要忘记在适当的地方从通知中心删除观察者

 [[NSNotificationCenter defaultCenter] removeObserver:self]; 

现在我们已经到了它的核心。 我们在keyboardWillShow方法中所要做的就是定位键盘视图并将其添加到我们的button中。
键盘视图是我们应用程序的第二个UIWindow的一部分,正如其他人已经想到的(请参阅此主题)。
所以我们对该窗口进行引用(在大多数情况下它将是第二个窗口,因此下面的代码中的objectAtIndex:1是正确的),遍历它的视图层次结构,直到find键盘并将button添加到其左下angular为止:

 - (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 view found; add the custom button to it if([[keyboard description] hasPrefix:@"UIKeyboard"] == YES) [keyboard addSubview:doneButton]; } } 

Voilà,就是这样!
我们button的空白空间从坐标(0,163)开始,并具有尺寸(106,53)。
doneButton方法当然必须写,但这不难。 只要确保在正在编辑的文本字段上调用resignFirstResponder以使键盘向下滑动即可。

我们是“完成”的。