完成按键仅用于数字键盘 [英] done button only for numberpad keyboard

查看:117
本文介绍了完成按键仅用于数字键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am working on the iPhone application. I have four textfields in one of the view. In these four textfield I have one textfield in which there will be use of numberpad keyboard. But on numberpad keyboard there is no return key. So I have added "Done"button programmatically by using following code.

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

     }

这个特定的文本字段没问题。但是这个按钮也可以添加到其他文本字段中。如何取消隐藏其他文本字段的按钮。

It is fine for this particular textfield. But this button is also get add for the rest of the textfields. How could unhide this button for other textfields.

谢谢很多提前。

推荐答案

请求请求请求请请请不要这样做。 这太荒谬了。考虑一下:

Please please please please please please please please please please please please please do not do this. This is so ridiculously fragile. Consider this:


  • 如果数字键盘的布局发生变化怎么办?

  • 如果颜色怎么办?键的更改?

  • 如果键盘的实现发生变化,使其不再是索引1处的窗口,该怎么办?

  • 如果执行该怎么办?键盘和外围设备主机的更改,以便内省的描述中断?

  • 如果你在iPad上运行这个键盘有一个完全不同的布局怎么办?

  • what if the layout of the number keypad changes?
  • what if the colors of the keys change?
  • what if the implementation of the keyboard changes such that it's no longer the window at index 1?
  • what if the implementation of the keyboard and peripheral host change such that introspecting the description breaks?
  • what if you're running this on iPad where the keyboard has a totally different layout?

这些只是将你的用户界面变成私人视图层次结构所带来的无数问题。

These are just a couple of the myriad of problems that come with glomming your UI into private view hierarchies.

以下是2个替代方案:


  1. 在每个 UIResponder 上使用 -inputAccessoryView 属性(因此每个 UIView )使用完成按钮定义 UIToolbar 。当键盘进入和退出时, inputAccessoryView 将位于键盘上方。

  1. Use the -inputAccessoryView property on every UIResponder (and thus every UIView) to define a UIToolbar with a "Done" button. The inputAccessoryView will be positioned above the keyboard as the keyboard animates in and out.

添加透明整个用户界面上的 UIView ,用于捕捉点按事件并导致键盘解除

Add a transparent UIView over your entire UI that captures tap events and causes the keyboard to dismiss

这篇关于完成按键仅用于数字键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆