【问题标题】:how to pop up datePicker when text filed click and disappear when editing done如何在文本字段单击时弹出 datePicker 并在编辑完成后消失
【发布时间】:2013-03-23 13:02:17
【问题描述】:

我看到了类似下面的东西

日期选择器视图总是在那里我怎样才能让它在点击输入日期时弹出,当我点击背景时,日期选择器应该下降

我刚刚习惯了日期选择器视图,但我不知道如何做到这一点出现和消失的东西

【问题讨论】:

    标签: ios uitextfield uipickerview


    【解决方案1】:

    好的。这是您对动画的要求的一些示例代码。

    - (void) showView
    {
        [self.view addSubview:yourDatePickerView];
            yourDatePickerView.frame = CGRectMake(0, -250, 320, 50);
            [UIView animateWithDuration:1.0
                             animations:^{
                                 yourDatePickerView.frame = CGRectMake(0, 152, 320, 260);
                             }];
    }
    

    这里是如何隐藏你的 DatePickerView

    - (void) hideView
    {
        [UIView animateWithDuration:0.5
                             animations:^{
                                 yourDatePickerView.frame = CGRectMake(0, -250, 320, 50);
                             } completion:^(BOOL finished) {
                                 [yourDatePickerView removeFromSuperview];
                             }];
    }
    
    - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
    {
        if(textField == yourDateTextField)
        {
             [self showView];
             return NO; // preventing keyboard from showing
        }
        return YES;
    }
    
    
    - (void)textFieldDidEndEditing:(UITextField *)textField
    {
        if(textField == yourDateTextField)
        {
             [self hideView];
        }
    }
    

    这就是你所需要的。

    【讨论】:

      【解决方案2】:

      您应该在委托方法中识别文本字段。

      - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
      {
          if(textField == DATE_TEXT_FIELD)
          {
                   //Display date picker
          }
      }
      
      
      - (void)textFieldDidEndEditing:(UITextField *)textField
      {
      
              if(textField == DATE_TEXT_FIELD)
              {
                       //Hide date picker
                  }
      }
      

      【讨论】:

        【解决方案3】:
        • 创建一个包含此日期选择器视图的UIView 引用。
        • 现在将此datePickerContainerView 分配给textField.inputView 财产。
        • 然后将textfield.delegate 分配给self 并实现您的 textFieldShouldReturn 方法。在那个方法中,写下这几行

          [textField resignFirstResponder];

          返回是;

        现在,当您点击该文本字段时,它将根据 inputview 属性设置加载 datepicker 视图来代替默认键盘。

        【讨论】:

          【解决方案4】:

          在为 datePicker 编写代码之前,请确保键入以下代码行

              [textField resignFirstResponder];
          

          我们知道,键盘是文本字段的默认第一响应者。所以你应该先退出它,然后为选择器编码,否则键盘也会弹出。

          【讨论】:

            猜你喜欢
            • 2021-03-27
            • 2022-09-27
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-07-19
            • 1970-01-01
            • 2021-10-08
            • 1970-01-01
            相关资源
            最近更新 更多