UIKeyboardTypeNumberPad关闭键盘

时间:2011-06-01 06:42:57

标签: objective-c cocoa-touch ios

如果键盘的类型为UIKeyboardTypeNumberPad,用户如何表示他/她已完成输入?没有返回/完成按钮,所以我怎么知道用户想要解雇键盘? 感谢

3 个答案:

答案 0 :(得分:2)

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

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

以编程方式创建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.
}

答案 1 :(得分:1)

您可以提供取消/隐藏按钮(在键盘外)或在键盘外的任何位置触摸时隐藏。这两个是常见的做法。

答案 2 :(得分:0)

试试这个。它可能会帮助你 触摸屏幕时隐藏键盘。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

}