2011-04-29 163 views
9

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

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

回答

11

你可以这样做:

@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; 
} 
+2

对于iOS 6.1,这不工作,除非我初始化工具栏如下所示:'UIToolbar * toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,self.view.bounds.size.width,44)];'否则,完美答案 – Segfault 2013-04-22 19:24:12

+0

@Segfault确认。您需要指定一个框架才能在6.1中正常工作。 – Paul 2013-06-22 12:40:13

+0

感谢Segfault,我会编辑它。 – Oscar 2013-07-19 03:12:00

1

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

if (there is a touch outside of your textField) 
{ 

    [textField resignFirstResponder]; 

} 
+0

但我想在数字键盘上创建一个完成按钮,所以如何在数字键盘上创建完成按钮。如果它是可行的,那么请提供帮助。 – jaydev 2011-05-01 06:42:45

+0

对不起,但这是不可能与iPhone sdk使用私人API'你必须去一些替代解决方案,你可以创建自己的键盘上完成按钮.. – Saurabh 2011-05-01 09:23:19

6

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

0

这个问题有已经回答了,我知道,因为那是我如何得到解决方案。您有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]; 
       } 
      } 
     } 

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

顺便说一句我粘贴的代码在我的布局上工作。对于你的你可能需要修改一下,但原理是一样的。

  • 创建按钮

  • 查找键盘子视图

  • 按钮添加到子视图

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

}