UIDatePicker、时间和 1970 年

发布于 2024-11-08 18:44:53 字数 827 浏览 3 评论 0原文

我遇到了一个小问题,我已经思考了一两天了。

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

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

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

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

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

我的问题是,我能做些什么来解决这个问题?

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

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

我还尝试过 NSDateFormatter:stringFromDate|dateFromString 调用,这些没有任何影响。

我还没有做的事情:

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

重建 UIDatePicker:从头开始的时间

我是否忽略了什么?

谢谢。

I've come across a small issue that I've been chewing on for a day or two now.

Using the Apple example project called DateCell I lifted its UIDatePicker and set it to time. I used this particular code as it did the animated slide on/off the screen.

My workflow is to set four values, start time, lunch time out, lunch time in, and stop time. I set these values by using [NSDate date].

I then use NSCalander:component calls to do some math such as "add 30 minutes to start time to get lunch time out," and "start time - lunch time out - 8 hours to get stop time."

The initial setup goes just fine. Clicking on the start time cell brings up the picker, and selecting a time change the other three times following my simple math formula's.

If the second row is selected, the lunch time out, the wheel comes up again to pick your lunch time out time. However this is where my problems start. It seems that my UIDatePickerModeTime wheel returns a date portion for January 1, 1970. And its this date portion that messes up my math formulas.

My question is, what can I do to fix this?

I've tried setting an initial, minimum time, in the XIB for the picker. This sort of works but when you pick a time on the wheel, the wheel spins itself to the time set in the XIB. This method doesn't have a clean feel to it.

I've tried settings the initWithTimeInterval class methods, but these block out times and isn't what I'm looking for I think.

I've also tried the NSDateFormatter:stringFromDate|dateFromString calls, and these had no affect.

What I have not done yet:

Custom defined date/time string.

Rebuilding UIDatePicker:Time from scratch

Am I over looking anything?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

骄兵必败 2024-11-15 18:44:53

我已经解决了我的问题,这就是我的解决方法。

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

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

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

我必须找到一种新的方法来进行数学计算并操纵从选择器中获得的时间。

我最终使用的是 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。从视觉上看,短语“下班”将位于该行的左侧,时间将位于右侧。

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

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

我执行的下一个操作是将时钟设置为我们工作日结束的时间。为此,我使用 dateByAddingTimeInterval 操作,它也是一个 NSDate 方法,因此它返回的任何内容都将采用日期/时间格式。在此操作中,我们将remainingInTheDay(10,800 秒)添加到inFromLunch(例如上午11:30)。我们的时钟输出时间现在是下午 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];
} 

在此示例中,我们之前已确定选择了与“打卡上班”时间相关的行(如果愿意,请“触摸内部”),然后我们转到此方法。

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

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

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

简而言之,我使用了错误的方法来处理我的拣选机正在转动的东西。

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

I've solved my problem, and here is how I addressed it.

Before I get to my answer I'm still really new to Objective-C and object oriented programming so my vocabulary doesn't know how to describe some of the things I've tried explaining. So take this into account when reading this.

Using UIDatePicker in time mode, i.e. you go into your NIB/XIB file and set your UIDatePicker object to 'time', will only return time. This is where I went wrong.

Using the NSDateComponent or any of the NSCalendar methods will bring out the date component of the picker. Thus you'll see January 1st, 1970 in the NSLog returns for example.

I had to find a new way of doing my math and manipulation of the times I was getting from the picker.

What I ended up using is NSTimeInterval, dateByAddingTimeInterval, and timeIntervalSinceDate. Research showed that NSTimeInterval is also a float type, so I used float to do some math as well.

Here an example -

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;
    }

I'm using a TableView to display my fields. When this 'if' statement is tripped it will populate the detailTextLabel of the line displaying "Clock Out." Visually the phrase "Clock Out" will be on the left side of the row, the time will be on the right side.

diffClockInOutToLunch is defined as a NSTimeInterval type. The operation being performed is timeIntervalSinceDate which essentially subtracts the value of outToLunch from the value of clockIn. Imagine outToLunch as being 11:00pm and clockIn as being 6:00am. This difference is 5 hours. NSTimeInterval stores values as seconds only so this difference of 5 hours is 18000 seconds.

I then perform a normal math operation using float. In this case I want to find out how many hours remain in the work day. This assumes the hours worked in a day is 8 hours. Because NSTimeInterval returns seconds, I converted 8 hours into seconds (28,800 seconds) and then subtract diffClockInOutToLunch from 28800. Now remainingInTheDay is equal to to 10800, or 3 hours.

The next operation I perform is set clockOut to the time our work day is finished. To do this I use the dateByAddingTimeInterval operation, which also is a NSDate method, so whatever it returns will be in a date/time format. In this operation we add remainingInTheDay (10,800 seconds) to inFromLunch (11:30am for example). Our clockOut time is now 2:30pm which is then sent through my DateFormatter and returned as a string to the cell of the TableView and also stored for later use.

Here's another example, from further down in my code -

- (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];
} 

In this example, we've previously determined that the row pertaining to "Clock In" time was selected ("Touch Up Inside" if you will) and we drop to this method.

What happens in this method is whenever clockIn is changed using the picker, the times displayed in outToLunch, inFromLunch, and clockOut automatically update and are displayed.

This example shows that we capture the value on the picker (self.pickerView.date) as clockIn. We then use clockIn to seed our mess of dateByAddingTimeInterval's and so forth.

So. This is how I managed my times using UIDatePicker (which is set to time mode).

The short answer would be I was using the wrong methods to work with what my picker was turning.

I hope this helps you and hopefully it'll be here if I need it again too ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文