【问题标题】:Can't Select today's date on uidatepicker ( already pre-selected)无法在 uidatepicker 上选择今天的日期(已预先选择)
【发布时间】:2013-02-13 17:33:50
【问题描述】:

我在我的 IOS 应用程序中添加了一个 UIDatePicker,默认情况下加载它并选择今天的日期。

我正在收听选定的事件以更新我的字段,但由于今天已经被选中,我从来没有得到它今天是我想要选择的日期(因为它已经被选中)。我需要将至少一列更改为不同的列,然后在第二步中选择返回今天的日期。

无论如何要在没有选定日期的情况下显示日期选择器,或者以某种方式收听选择器上的选项卡而不是选择? 不想在工具栏中添加“今天”按钮以添加任何外部控件来解决此问题,或者从预先选择的不同日期开始,因为它与预先选择的日期相同。

谢谢

【问题讨论】:

  • 只要在 viewDidLoad 中调用监听器调用的任何方法。
  • 我想要的是,一旦选择器以今天作为所选日期出现,如果用户在同一日期进行标签,则关闭日期选择器并将今天的日期作为所选日期发送。

标签: iphone ios ipad uidatepicker


【解决方案1】:

我不能 100% 确定您是否正在寻找这个。但这对我来说很好。

在viewcontroller.m中

- (void)viewDidLoad
 {
  [super viewDidLoad];
  dp.date = [NSDate date];    //dp is datepicker object
  NSDateFormatter *formDay = [[NSDateFormatter alloc] init];
  [formDay setDateFormat:@"dd/MM/yyy HH:mm"];
  NSString *day = [formDay stringFromDate:[dp date]];
  txt.text = day;
 }


-(IBAction)datepick:(id)sender
 {
  NSDateFormatter *formDay = [[NSDateFormatter alloc] init];
  [formDay setDateFormat:@"dd/MM/yyy HH:mm"];
  NSString *day = [formDay stringFromDate:[dp date]];
  txt.text = day; 
 }

将该方法连接到日期选择器的值更改事件并包含UITextFieldDelegate

当视图被加载时,

日期选择器滚动时

【讨论】:

  • 没有。基本上日期选择器将打开已选择今天的日期,但无法从第一个选项卡中说今天是我想要选择的。
【解决方案2】:

Apple 没有为此实施任何通知。原因是用户将点击、旋转和更改日期选择器,直到找到他们想要的日期。然后他们会点击某种完成或下一步按钮。您不希望用户尝试选择日期并且选择器继续解雇。如果你真的想实现它,那么你可以继承 UIDatePicker 或捕获你想要的部分。

【讨论】:

    【解决方案3】:

    尝试继承 UIDatePicker,并覆盖 hitTest:withEvent:

    -(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
    {
        // intercept the user touching the initially presented date on a datePicker,
        // as this will normally not send an event.
        // here we check if the user has touched the middle section of the picker,
        // then send the UIControlEventValueChanged action
    
        CGFloat midY = CGRectGetMidY(self.bounds);
        CGFloat dY = fabs(midY - point.y);
    
        if (dY < 17.0) // the active section is around 36px high
        {
            NSSet *targets = self.allTargets;
            for (id target in targets)
            {
                NSArray *actions = [self actionsForTarget:target forControlEvent:UIControlEventValueChanged];
    
                for (NSString *action in actions)
                {
                    // suppress the leak warning here as we're not returning anything anyway
    #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
                    [target performSelector:NSSelectorFromString(action) withObject:self];
                }
            }
        }
    
        return [super hitTest:point withEvent:event];
    }
    

    我确信它可以改进,但作为一个起点,它可能会对你有所帮助。

    【讨论】:

      猜你喜欢
      • 2017-07-08
      • 2017-07-07
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-22
      • 1970-01-01
      相关资源
      最近更新 更多