首页 文章

't Select today'可以在uidatepicker上预约(已经预选)

提问于
浏览
1

我在我的IOS应用程序中添加了一个UIDatePicker,默认情况下会加载今天的日期 .

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

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

谢谢

3 回答

  • 0

    我不是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

    加载视图时

    When the page is loading

    当日期选择器滚动时

    When datepicker is rolled

  • 3

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

  • 0

    尝试继承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];
    }
    

    我相信它可以改进,但作为一个起点它可能会帮助你 .

相关问题