如何在iPhone中隐藏数字键盘

时间:2014-03-07 11:03:05

标签: ios iphone

我有两个文本字段,一个用于用户名,第二个用于移动电话号码

用户名:  我可以使用此方法隐藏用户名Text-Fields的键盘

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

我的问题是 手机号码:

如何隐藏移动电话号码文字字段?

4 个答案:

答案 0 :(得分:5)

您可以使用以下library在数字键盘上添加完成和取消按钮。

此外,您可以使用点按手势隐藏数字键盘。使用以下代码:

 - (void)viewDidLoad
{

UITapGestureRecognizer* tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismissKeyboard)];
tapGesture.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:tapGesture];
}

-(void)dismissKeyboard
 {
 [textField resignFirstResponder];//Assuming that UITextField Object is textField
 }

这对你有用..快乐的编码!!干杯.. !!

答案 1 :(得分:1)

 - (void)viewDidLoad
{
    [self addTapGesture];
}

#pragma mark
#pragma mark Gesture  methods

- (void) addTapGesture {
    UITapGestureRecognizer *tapper = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
    tapper.cancelsTouchesInView = FALSE;
    [self.view addGestureRecognizer:tapper];
}

- (void)handleSingleTap:(UITapGestureRecognizer *) sender
{
    [self.view endEditing:YES];
}

答案 2 :(得分:0)

使用

textField.keyboardType = UIKeyboardTypeNumberPad;

答案 3 :(得分:0)

您可以选择几个选项,您需要决定何时关闭文本字段数字键盘。例如,如果您在解雇之前需要一定数量的数字,则可以订阅

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textFieldTextDidChange:) name:UITextFieldTextDidChangeNotification object:nil];

然后按照

的方式实施
-(void)textFieldTextDidChange:(NSNotification *)notification
{
    UITextField *textfield = notification.object;

    if(textfield.text.length == NUMBER_OF_DIGITS_NEEDED)
        [textField resignFirstResponder];
}

如果您的用户还需要自行决定关闭键盘,则应在剩余的可见视图上放置一个水龙头手势识别器。

UITapGestureRecognizer *tapGestureRecogniser = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
tapGestureRecogniser.delegate = self;
[self.view addGestureRecognizer:tapGestureRecogniser];

-(void)handleTapGesture:(UITapGestureRecognizer *)tapGestureRecogniser
{
    if ([self.myTextField isFirstResponder])
        [self.myTextField resignFirstResponder];
}