【问题标题】:UIDatePicker, time, and 1970UIDatePicker、时间和 1970
【发布时间】:2011-08-29 10:54:58
【问题描述】:

我遇到了一个小问题,我已经纠结了一两天了。

使用名为 DateCell 的 Apple 示例项目,我提升了它的 UIDatePicker 并将其设置为时间。我使用这个特殊的代码,因为它在屏幕上/关闭动画幻灯片。

我的工作流程是设置四个值,开始时间、午餐时间、午餐时间和停止时间。我使用 [NSDate date] 设置这些值。

然后我使用 NSCalander:component 调用进行一些数学运算,例如“增加 30 分钟开始时间以获得午餐时间”和“开始时间 - 午餐时间结束 - 8 小时以获得停止时间”。

初始设置运行良好。单击开始时间单元格会弹出选择器,然后按照我的简单数学公式选择一个时间更改其他三次。

如果选择了第二行,午餐时间结束,轮子再次出现以选择您的午餐时间结束时间。然而,这是我的问题开始的地方。似乎我的 UIDatePickerModeTime 轮返回了 1970 年 1 月 1 日的日期部分。它的这个日期部分弄乱了我的数学公式。

我的问题是,我该怎么做才能解决这个问题?

我尝试在 XIB 中为选择器设置一个初始的最短时间。这种方法很有效,但是当您在轮盘上选择时间时,轮盘会自行旋转到 XIB 中设置的时间。这种方法没有干净的感觉。

我已经尝试设置 initWithTimeInterval 类方法,但这些阻塞时间并不是我想要的。

我也尝试过 NSDateFormatter:stringFromDate|dateFromString 调用,这些都没有影响。

我还没有做的事情:

自定义日期/时间字符串。

重建UIDatePicker:时间从头开始

我是不是在看什么?

谢谢。

【问题讨论】:

    标签: iphone ios uidatepicker uidatepickermodetime


    【解决方案1】:

    我已经解决了我的问题,下面是我的解决方法。

    在我得到答案之前,我对 Objective-C 和面向对象编程还是很陌生,所以我的词汇不知道如何描述我尝试解释过的一些事情。所以在阅读本文时要考虑到这一点。

    在时间模式下使用 UIDatePicker,即您进入您的 NIB/XIB 文件并将您的 UIDatePicker 对象设置为“时间”,只会返回时间。这就是我出错的地方。

    使用 NSDateComponent 或任何 NSCalendar 方法将显示选择器的日期组件。例如,您会在 NSLog 返回中看到 1970 年 1 月 1 日。

    我必须找到一种新的方法来计算我从选择器获得的时间。

    我最终使用的是 NSTimeInterval、dateByAddingTimeInterval 和 timeIntervalSinceDate。研究表明 NSTimeInterval 也是一个浮点类型,所以我也用浮点数来做一些数学运算。

    这里是一个例子-

    if (indexPath.row == [labelArray indexOfObjectIdenticalTo:@"Clock Out"])
        {        
            NSTimeInterval diffClockInOutToLunch = [outToLunch timeIntervalSinceDate:clockIn];
            float remainingInTheDay = 28800 - diffClockInOutToLunch;
    
            self.clockOut = [inFromLunch dateByAddingTimeInterval:remainingInTheDay];
    
            cell.detailTextLabel.text = [self.dateFormatter stringFromDate:clockOut];
    
            self.clockOutIndex = indexPath;
    
            return cell;
        }
    

    我正在使用 TableView 来显示我的字段。当这个“if”语句被触发时,它将填充显示“Clock Out”的行的detailTextLabel。从视觉上看,“Clock Out”这一短语将位于行的左侧,时间将位于右侧。

    diffClockInOutToLunch 被定义为 NSTimeInterval 类型。正在执行的操作是 timeIntervalSinceDate,它实质上是从 clockIn 的值中减去 outToLunch 的值。想象 outToLunch 是晚上 11:00,而 clockIn 是早上 6:00。这个差是5个小时。 NSTimeInterval 仅将值存储为秒,因此 5 小时的差异为 18000 秒。

    然后我使用浮点数执行正常的数学运算。在这种情况下,我想知道工作日还剩多少小时。这假设一天的工作时间是 8 小时。因为 NSTimeInterval 返回秒,所以我将 8 小时转换为秒(28,800 秒),然后从 28800 中减去 diffClockInOutToLunch。现在剩余的InTheDay 等于 10800,即 3 小时。

    我执行的下一个操作是将 clockOut 设置为我们工作日结束的时间。为此,我使用 dateByAddingTimeInterval 操作,它也是一个 NSDate 方法,因此它返回的任何内容都将采用日期/时间格式。在此操作中,我们将剩余的InTheDay(10,800 秒)添加到 inFromLunch(例如上午 11:30)。我们的 clockOut 时间现在是下午 2:30,然后通过我的 DateFormatter 发送并作为字符串返回到 TableView 的单元格,并存储以供以后使用。

    这是另一个示例,来自我的代码的更下方 -

    - (void)clockInChanged
    {
        // Set clockIn value
        self.clockIn = self.pickerView.date;
    
        // Change the outToLunch time
        self.outToLunch = [self.pickerView.date dateByAddingTimeInterval:5*60*60];
    
        UITableViewCell *outToLunchCell = [self.tableView cellForRowAtIndexPath:outToLunchIndex];
        outToLunchCell.detailTextLabel.text = [self.dateFormatter stringFromDate:outToLunch];
    
        // Change the inFromLunch time
        self.inFromLunch = [outToLunch dateByAddingTimeInterval:30*60];
    
        UITableViewCell *inFromLunchCell = [self.tableView cellForRowAtIndexPath:inFromLunchIndex];
        inFromLunchCell.detailTextLabel.text = [self.dateFormatter stringFromDate:inFromLunch];
    
        // Change the clockOut time
        NSTimeInterval diffClockInOutToLunch = [outToLunch timeIntervalSinceDate:clockIn];
        float remainingInTheDay = 28800 - diffClockInOutToLunch;
    
        self.clockOut = [inFromLunch dateByAddingTimeInterval:remainingInTheDay];
        UITableViewCell *clockOutCell = [self.tableView cellForRowAtIndexPath:clockOutIndex];
        clockOutCell.detailTextLabel.text = [self.dateFormatter stringFromDate:clockOut];
    } 
    

    在此示例中,我们之前已确定选择了与“打卡”时间相关的行(如果您愿意,可以选择“Touch Up Inside”),然后我们就使用此方法。

    在这个方法中发生的事情是,每当使用选择器更改 clockIn 时,outToLunch、inFromLunch 和 clockOut 中显示的时间会自动更新并显示。

    此示例显示我们将选取器 (self.pickerView.date) 上的值捕获为 clockIn。然后我们使用 clockIn 来播种我们的 dateByAddingTimeInterval 等等。

    所以。这就是我使用 UIDatePicker(设置为时间模式)管理时间的方式。

    简短的回答是我使用了错误的方法来处理我的选择器正在转动的东西。

    我希望这对您有所帮助,如果我再次需要它,希望它会在这里;)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多