如何在UIKeyboardTypeDecimalPad中包含返回按钮?

时间:2013-03-21 18:53:40

标签: xcode io uitextfield uitextfielddelegate uikeyboardtype

我实现了一个UITextField,它应该只允许用户包含十进制数字。我已将键盘类型更改为UIKeyboardTypeDecimalPad,但此键盘布局上没有返回按钮以退出文本字段。我不想在键盘上添加额外的控件,因为它看起来很乱。如何更改dot按钮以返回并更改其功能,还是有其他选择?

1 个答案:

答案 0 :(得分:9)

默认情况下,返回键不带Decimal键盘类型。使用以下代码段来实现:

UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 35.0f)];
    toolbar.barStyle=UIBarStyleBlackOpaque;

    // Create a flexible space to align buttons to the right
    UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

    // Create a cancel button to dismiss the keyboard
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(resetView)];

    // Add buttons to the toolbar
    [toolbar setItems:[NSArray arrayWithObjects:flexibleSpace, barButtonItem, nil]];

    // Set the toolbar as accessory view of an UITextField object
    _yourTextField.inputAccessoryView = toolbar;

希望这会有所帮助。快乐的编码!!