完成按键仅用于数字键盘

Nit*_*tin 2 iphone objective-c ios

我正在研究iPhone应用程序.我在其中一个视图中有四个文本字段.在这四个文本字段中,我有一个文本字段,其中将使用数字键盘.但在数字键盘上没有返回键.所以我通过使用以下代码以编程方式添加了"完成"按钮.

    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
    [[NSNotificationCenter defaultCenter] addObserver:self                  
            selector:@selector(keyboardDidShow:) 
                                                 name:UIKeyboardDidShowNotification object:nil];        
} else {
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(keyboardWillShow:) 
                                                 name:UIKeyboardWillShowNotification object:nil];
}

   - (void)addButtonToKeyboard {
// create custom button
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
doneButton.frame = CGRectMake(0, 163, 106, 53);
doneButton.adjustsImageWhenHighlighted = NO;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.0) {
    [doneButton setImage:[UIImage imageNamed:@"DoneUp3.png"] forState:UIControlStateNormal];
    [doneButton setImage:[UIImage imageNamed:@"DoneDown3.png"] forState:UIControlStateHighlighted];
} else {        
    [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];
        }
     }
    }

     - (void)keyboardWillShow:(NSNotification *)note {
// if clause is just an additional precaution, you could also dismiss it
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 3.2) {

        [self addButtonToKeyboard];


}
  }

       - (void)keyboardDidShow:(NSNotification *)note {
// if clause is just an additional precaution, you could also dismiss it
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {

        [self addButtonToKeyboard];


      }
   }


- (void)doneButton:(id)sender {
    NSLog(@"doneButton");
    NSLog(@"Input: %@", contactno.text);

    [contactno resignFirstResponder];

     }
Run Code Online (Sandbox Code Playgroud)

这个特定的文本域很好.但是这个按钮也可以添加到其他文本字段中.如何取消隐藏其他文本字段的按钮. 在此输入图像描述

非常感谢.

Dav*_*ong 23

请,请,请,请,请,请请,请请请请不要这样做. 这太荒谬了.考虑一下:

  • 如果数字键盘的布局发生变化怎么办?
  • 如果键的颜色改变了怎么办?
  • 如果键盘的实现发生变化,使其不再是索引1处的窗口,该怎么办?
  • 如果键盘和外围主机的实现发生变化,以便内省的描述中断了怎么办?
  • 如果你在iPad上运行这个键盘有一个完全不同的布局怎么办?

这些只是将您的UI重写为私有视图层次结构所带来的无数问题.

不要这样做.

以下是两种选择:

  1. -inputAccessoryView在每个UIResponder(因此每个UIView)上使用该属性来定义UIToolbar带有"完成"按钮的属性.当inputAccessoryView键盘进出动画时,它将位于键盘上方.

  2. UIView在整个UI上添加透明,捕获点击事件并使键盘解除

  • 3.提交错误报告,要求Apple在数字键盘上添加一个怪异的完成按钮. (15认同)