【问题标题】:how to hide number pad in iphone如何在iphone中隐藏数字键盘
【发布时间】:2014-04-10 11:54:20
【问题描述】:

我有两个文本字段,一个是用户名,第二个是手机号码

用户名: 我可以使用此方法隐藏用户名文本字段的键盘

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

我的问题是 手机号码:

如何隐藏手机号码文本框?

【问题讨论】:

标签: ios iphone


【解决方案1】:

使用

textField.keyboardType = UIKeyboardTypeNumberPad;

【讨论】:

  • 从给出的代码来看,很明显他想用回车键隐藏键盘,但是数字键盘没有回车键,所以提出了问题。
【解决方案2】:

您可以使用以下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
 }

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

【讨论】:

    【解决方案3】:

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

        [[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];
    }
    

    【讨论】:

      【解决方案4】:
       - (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];
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-19
        • 2011-02-22
        • 1970-01-01
        • 2011-04-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多