objective-c - UIKeyboardTypeNumberPad 关闭键盘

标签 objective-c cocoa-touch ios

如果键盘是 UIKeyboardTypeNumberPad 类型,用户应该如何指示他/她已完成输入?没有返回/完成按钮,所以我怎么知道用户想要关闭键盘?
谢谢

最佳答案

在研究了这个问题一段时间后,我没有找到真正令人满意的答案。一些解决方案破解了“完成”按钮,但这不适用于本地化应用程序。我的应用程序不使用任何单词,所以我绝对不想要“完成”按钮。

正如 taskinoor 所提到的,如果在其他地方触摸 View ,则可以方便地结束编辑。这就是我所做的,这里是显示细节的代码:

以编程方式创建 textField:

aTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
[aTextField addTarget:self action:@selector(c1Update) forControlEvents:UIControlEventEditingDidEnd];
aTextField.keyboardType = UIKeyboardTypeNumberPad;
[yourView addSubview:aTextField];
c1Value = aTextField; // I like to keep a pointer for UI updates, alpha etc.

c1Update 中会发生什么:
- (void)c1Update {
    [[self brain] setC1:[c1Value.text intValue]]; // update the model
}

手势识别器显示:
- (void)viewDidLoad {
    [super viewDidLoad];
    [self updateViewColors];

    UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped)];
    tapRecognizer.numberOfTapsRequired = 1;
    [self.view addGestureRecognizer:tapRecognizer];
}

- (void) viewTapped {
    if (c1Value.isFirstResponder) {[c1Value resignFirstResponder];} // dismisses keyboard, which causes c1Update to invoke.
}

关于objective-c - UIKeyboardTypeNumberPad 关闭键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6197249/

相关文章:

iphone - 在 3/4 行之后,scrollToRowAtIndexPath 未滚动到正确的行

ios - AVPlayer 中的视频不会以与播放器本身相同的速率改变大小

ios - 更新 EKEvent 时收到间歇性 EXC_BAD_ACCESS

ios - 无法向服务 com.apple.WebKit.WebContent 发送信号

ios - 向现有本地化应用程序添加新语言

ios - 某些电子邮件应用程序禁止向其他人发送带有自定义 OpenURL 方案的链接

ios - touchesMoved 在特定的移动对象上

objective-c - 在 iOS 上加载 UIView 时如何运行一些代码?

ios - 无法删除 UITableView 中的行

ios - OpenCV 2.4.3 (iOS) 中的 Crop Mat 图像