数字键盘键盘在iPhone中不显示返回键

时间:2011-04-29 14:13:48

标签: iphone ios-4.2

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

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

5 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:6)

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

答案 2 :(得分:1)

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

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

   [textField resignFirstResponder];

}

答案 3 :(得分: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];
                }
            }
        } 

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

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

  • 创建按钮

  • 查找键盘子视图

  • 将按钮添加到该子视图

答案 4 :(得分: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];

}