【问题标题】:number pad keyboard not show return key in iphoneiPhone中的数字键盘不显示返回键
【发布时间】:2011-08-15 13:10:09
【问题描述】:

我是 iPhone 技术的新手。我在 iPhone 应用程序中保存了一个问题,但是当我使用文本字段键盘类型数字键盘时,键盘不显示返回/完成按钮。

在文本字段中输入数字后如何隐藏数字键盘?你有什么解决方案。请提供帮助。

【问题讨论】:

    标签: iphone ios-4.2


    【解决方案1】:
    - (void)viewDidLoad
    {
    
        [super viewDidLoad];
        UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
        numberToolbar.barStyle = UIBarStyleBlackTranslucent;
        numberToolbar.items = [NSArray arrayWithObjects:
                               [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
                               [[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(doneWithNumberPad)],
                               nil];
        [numberToolbar sizeToFit];
    
        _txt_mobileno.inputAccessoryView = numberToolbar;
    
    }
    
    -(void)doneWithNumberPad
    {
        [_txt_mobileno resignFirstResponder];
    
    }
    

    【讨论】:

      【解决方案2】:

      你可以这样做:

      @property (nonatomic, strong) UITextField *textField;
      
      - (void)viewDidLoad {
          [super viewDidLoad];
      
          self.textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
          UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonDidPressed:)];
          UIBarButtonItem *flexableItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:NULL];
          UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[self class] toobarHeight])];
          [toolbar setItems:[NSArray arrayWithObjects:flexableItem,doneItem, nil]];
          self.textField.inputAccessoryView = toolbar;
      }
      
      - (void)doneButtonDidPressed:(id)sender {
          [self.textField resignFirstResponder];
      }
      
      + (CGFloat)toolbarHeight {
          // This method will handle the case that the height of toolbar may change in future iOS.
          return 44.f;
      }
      

      【讨论】:

      • 对于 iOS 6.1,这不起作用,除非我用这样的框架初始化工具栏:UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)]; 否则,完美答案
      • @Segfault 确认。您需要指定一个框架才能使其在 6.1 中正常工作。
      【解决方案3】:

      这个问题已经得到解答,我知道,因为这就是我得到解决方案的方式。您有 2 个选项,首先是通过在主视图上实现触摸来隐藏键盘,该主视图将发送“完成编辑”消息。隐藏键盘 [self.view endEditing:YES];

      如果您确实将触摸侦听器添加到主视图,您必须实现一个条件,以便任何其他按钮继续工作。

      你想要模拟一个返回键是像这样实际添加它:

      注册键盘确实显示通知并将其添加到代码中:

      if ([self.passCodeLabel isFirstResponder])
              {
                  UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
                  doneButton.frame = CGRectMake(0, 163, 106, 53);
                  //doneButton.frame = CGRectMake(0, 163, 257, 257);
                  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];
                  NSLog(@"%@",[[UIApplication sharedApplication] windows]);
      
                  UIView* keyboard;
                  NSLog(@"Shared applicaiton windows count:%i",tempWindow.subviews.count);
                  for(int i=0; i<[tempWindow.subviews count]; i++) {
                      keyboard = [tempWindow.subviews objectAtIndex:i];
                      NSLog(@"%@",[keyboard description]);
                      // keyboard view found; add the custom button to it
                      if([[keyboard description] hasPrefix:@"<UIPeripheralHostView"] == YES)
                      {
                          NSLog(@"Adding return button");
                          [keyboard addSubview:doneButton];
                      }
                  }
              } 
      

      这会将您自己的“完成”按钮图像添加到键盘(您可以通过截取空白插槽的屏幕截图并添加完成文本来复制)。

      顺便说一句,我粘贴的代码适用于我的布局。对于你的,你可能需要稍微修改一下,但原理是一样的。

      • 创建按钮

      • 查找键盘子视图

      • 将按钮添加到该子视图

      【讨论】:

        【解决方案4】:

        你必须使用 UIKeyboardTypeNumberPad,试试这个 UIKeyboardTypeNumbersAndPunctuation, 它不仅会显示返回键,还会为您提供一些额外的标点符号

        【讨论】:

          【解决方案5】:

          数字键盘中没有返回或完成键。当用户在文本字段之外触摸时,您可以做一件事,您可以隐藏键盘。你应该做这样的事情 -

          if (there is a touch outside of your textField)
          {
          
             [textField resignFirstResponder];
          
          }
          

          【讨论】:

          • 但我想在数字键盘上创建一个完成按钮,那么如何在数字键盘上创建这个完成按钮。如果可能,请提供帮助。
          • 抱歉,iPhone sdk 无法使用私有 API,您必须使用一些替代解决方案,您可以创建自己的键盘,上面有完成按钮。..
          猜你喜欢
          • 1970-01-01
          • 2010-10-02
          • 2011-08-05
          • 1970-01-01
          • 2011-10-08
          • 2012-10-08
          • 1970-01-01
          • 1970-01-01
          • 2014-04-29
          相关资源
          最近更新 更多