【问题标题】:Disapperiang IPhone Numeric Keyboard消失的 iPhone 数字键盘
【发布时间】:2009-09-10 22:46:16
【问题描述】:

我如何在 iPhone 中消失 数字键盘,因为其中没有返回键,这意味着它不会响应

- (BOOL)textFieldShouldReturn:(UITextField *)textField

【问题讨论】:

  • 复制: - stackoverflow.com/questions/442491> - stackoverflow.com/questions/649777> - stackoverflow.com/questions/1273439> - stackoverflow.com/questions/804563> - stackoverflow.com/questions/963571> - stackoverflow.com/questions/1328230> - - stackoverflow.com/questions/1176877> 就在搜索结果的第一页...
  • 恐怕你错了,因为他们都在谈论当键盘不是数字类型时,我知道当它不是数字时如何禁用键盘,它的不同情况,当你必须消失时“数字键盘”。我在提出这个问题之前进行了搜索。
  • 您可以轻松地在屏幕上放置一个按钮,以便在用户输入数字后关闭键盘。手机应用程序不就是这样吗?或者你可以在键盘上方放置一个工具栏,上面有一个“don”按钮,或者任何数量的东西(在 x 秒不活动后自动关闭键盘,一旦字段中的文本验证等)
  • [theTextField resignFirstResponder];无论您使用什么键盘都可以使用。
  • 戴夫,你是一个好人,但你却沉迷于试图找到重复的答案。不过你真的很聪明。你能看看这个问题。 stackoverflow.com/questions/4360140/…

标签: iphone objective-c keypad


【解决方案1】:

在 touchesBegan 中使用 resignFirstResponder,如下所示:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch * touch = [touches anyObject];
    if(touch.phase == UITouchPhaseBegan) {
        [self.myTextField resignFirstResponder];
    }
}

如果您不确定当前焦点的位置,您可能需要在多个文本字段上调用它;我还没有测试过这种情况。

此外,在 Interface Builder 中,您需要为视图启用“启用用户交互”复选框,或以编程方式使用:

myView.userInteractionEnabled = YES;

【讨论】:

    【解决方案2】:

    这就是答案..

    -(void) touchesBegan :(NSSet *) touches withEvent:(UIEvent *)event
    
    {
    
        //[URL resignFirstResponder];
    
        //[Password resignFirstResponder];
    
        [speedField resignFirstResponder];
    
        [super touchesBegan:touches withEvent:event ];
    
    }
    

    【讨论】:

      【解决方案3】:

      试试这个:

      [theTextField resignFirstResponder];
      

      【讨论】:

      • 好吧,你在我上面写的函数中这样做,就像这样。无论如何我应该把这段代码放在哪里工作?
      • 在我看来,我有很多文本字段,而且它不适用于数字键盘
      【解决方案4】:

      为什么不使用“数字和标点”键盘而不是“数字”?这将节省您的时间

      【讨论】:

        【解决方案5】:

        给你。用这个扩展你的 UIViewController 类。但是我们怎么知道它是完成按钮呢? Adding Done Button to Only Number Pad Keyboard on iPhone

        //
        //  UIViewController+NumPadReturn.m
        //  iGenerateRandomNumbers
        //
        //  Created by  on 12/4/10.
        //  Copyright 2010 __MyCompanyName__. All rights reserved.
        //
        
        #import "UIViewController+NumPadReturn.h"
        
        
        @implementation UIViewController (NumPadReturn)
        
        -(void) viewDidLoad{
            // add observer for the respective notifications (depending on the os version)
            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)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)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)doneButton:(id)sender {
            NSLog(@"doneButton");
            [self.view endEditing:TRUE];
        }
        

        【讨论】:

        • -1 这完全是 hack,不应该使用。如果苹果决定将“UIKeyboard”的名称更改为UIKBView?,您永远不应该依赖未记录的行为或实现细节来使您的代码正常工作。如果您需要在没有未记录的东西的情况下无法实现的行为,请提交错误。
        猜你喜欢
        • 2016-12-18
        • 2010-10-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-23
        • 2010-12-23
        • 1970-01-01
        • 2010-11-09
        相关资源
        最近更新 更多