首页 文章

UIDatePicker,时间和1970年

提问于
浏览
1

我遇到了一个小问题,我现在已经咀嚼了一两天 .

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

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

然后,我使用NSCalander:组件调用来做一些数学运算,例如“添加30分钟开始时间让午餐时间结束”和“开始时间 - 午餐时间 - 8小时以获得停止时间” .

初始设置很好 . 单击开始时间单元格会调出选择器,选择时间会根据我的简单数学公式更改其他三次 .

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

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

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

我已经尝试过设置initWithTimeInterval类的方法,但是这些方法会阻塞时间而不是我想要的东西 .

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

我还没有完成的事情:

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

重建UIDatePicker:从头开始的时间

我在看什么吗?

谢谢 .

1 回答

  • 1

    我已经解决了我的问题,这就是我如何解决它的问题 .

    在我得到答案之前,我仍然是Objective-C和面向对象编程的新手,所以我的词汇不知道如何描述我试过的一些解释的东西 . 所以在阅读本文时请考虑到这一点 .

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

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

    我必须找到一种新的方式来做我的数学和操纵我从拾取器得到的时间 .

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

    这是一个例子 -

    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点,clockIn是早上6点 . 这个差异是5个小时 . NSTimeInterval仅将值存储为秒,因此5小时的差异为18000秒 .

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

    我执行的下一个操作是将clockOut设置为我们工作日结束的时间 . 为此,我使用dateByAddingTimeInterval操作,它也是一个NSDate方法,因此无论它返回什么,都将采用日期/时间格式 . 在此操作中,我们将remainingInTheDay(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];
    }
    

    在这个例子中,我们之前已经确定选择了与“Clock In”时间相关的行(如果你愿意的话,“Touch Up Inside”),我们将采用这种方法 .

    在这种方法中发生的是每当使用选择器改变clockIn时,时间显示在outToLunch,inFromLunch和clockOut中,并自动更新并显示 .

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

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

    简短的回答是我使用了错误的方法来处理我的选择器转向的问题 .

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

相关问题