UIDatePicker设置分钟间隔时的奇怪行为 [英] UIDatePicker odd behavior when setting minuteInterval

查看:98
本文介绍了UIDatePicker设置分钟间隔时的奇怪行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码在iOS 4.3(也许是其他版本)下显示了奇怪的行为.在此示例中,显示日期设置为4 Aug 2011 2:31 PMUIDatePicker. UIDatePicker下的UILabel显示参考日期.下面的三个UIButtons(标记为1、5、10)将minuteInterval设置在UIDatePicker上.

The following code displays an odd behavior under iOS 4.3 (maybe others version too). In this example, a UIDatePicker whose date is set to 4 Aug 2011 2:31 PM is displayed. The UILabel below the UIDatePicker displays the date for reference. The three UIButtons below, labeled 1, 5, 10 set the minuteInterval on the UIDatePicker.

点击1-将在UIDatePicker中选择的日期显示为4 Aug 2011 2:31 PM,并且分钟间隔为1,这是可以预期的.

Tapping 1 - shows the selected date in the UIDatePicker to be 4 Aug 2011 2:31 PM, and the minute interval is 1, which is to be expected.

点击5-将在UIDatePicker中选择的日期显示为4 Aug 2011 2:35 PM,分钟间隔为5,这是可以预期的(有人可能会说时间应该四舍五入,但这并不是一个大问题).

Tapping 5 - shows the selected date in the UIDatePicker to be 4 Aug 2011 2:35 PM, and the minute interval is 5, which is to be expected (one could argue the time should round down, but that is not a huge issue).

点击10-在UIDatePicker中将所选日期显示为4 Aug 2011 2:10 PM,分钟间隔为10.好的分钟间隔是正确的,但是所选时间为2:10?人们会期望2:40(如果向上舍入)或2:30(如果向下舍入).

Tapping 10 - shows the selected date in the UIDatePicker to be 4 Aug 2011 2:10 PM, and the minute interval is 10. Okay the minute interval is correct, but the selected time is 2:10? One would have expected 2:40 (if rounded up) or 2:30 (if rounded down).

BugDatePickerVC.h

#import <UIKit/UIKit.h>

@interface BugDatePickerVC : UIViewController {
    NSDateFormatter *dateFormatter;
    NSDate *date;
    UIDatePicker *datePicker;
    UILabel *dateL;
    UIButton *oneB;
    UIButton *fiveB;
    UIButton *tenB;
}

- (void) buttonEventTouchDown:(id)sender;

@end

BugDatePickerVC.m

@implementation BugDatePickerVC

- (id) init
{
    if ( !(self = [super init]) )
    {
        return self;
    }

    dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"d MMM yyyy h:mm a";

    date = [[dateFormatter dateFromString:@"4 Aug 2011 2:31 PM"] retain];

    // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
    // Date picker
    datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 216.0f)];
    datePicker.date = date;
    datePicker.minuteInterval = 1;
    [self.view addSubview:datePicker];

    // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
    // Label with the date.
    dateL = [[UILabel alloc] initWithFrame:CGRectMake(10.0f, 230.0f, 300.0f, 32.0f)];
    dateL.text = [dateFormatter stringFromDate:date];
    [self.view addSubview:dateL];

    // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
    // Button that set the date picker's minute interval to 1.
    oneB = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    oneB.frame = CGRectMake(10.0f, 270.0f, 100.0f, 32.0f);
    oneB.tag = 1;
    [oneB setTitle:@"1" forState:UIControlStateNormal];
    [oneB   addTarget:self
               action:@selector(buttonEventTouchDown:)
     forControlEvents:UIControlEventTouchDown];
    [self.view addSubview:oneB];

    // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
    // Button that set the date picker's minute interval to 5.
    fiveB = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    fiveB.frame = CGRectMake(10.0f, 310.0f, 100.0f, 32.0f);
    fiveB.tag = 5;
    [fiveB setTitle:@"5" forState:UIControlStateNormal];
    [fiveB  addTarget:self
               action:@selector(buttonEventTouchDown:)
     forControlEvents:UIControlEventTouchDown];
    [self.view addSubview:fiveB];

    // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
    // Button that set the date picker's minute interval to 10.
    tenB = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    tenB.frame = CGRectMake(10.0f, 350.0f, 100.0f, 32.0f);
    tenB.tag = 10;
    [tenB setTitle:@"10" forState:UIControlStateNormal];
    [tenB   addTarget:self
               action:@selector(buttonEventTouchDown:)
     forControlEvents:UIControlEventTouchDown];
    [self.view addSubview:tenB];

    return self;
}

- (void) dealloc
{
    [dateFormatter release];
    [date release];
    [datePicker release];
    [dateL release];
    [oneB release];
    [fiveB release];
    [tenB release];

    [super dealloc];
}

- (void) buttonEventTouchDown:(id)sender
{
    datePicker.minuteInterval = [sender tag];
}

推荐答案

好的,所以我可以通过使用以下代码将UIDatePicker日期值显式设置为四舍五入为分钟间隔的日期来更改行为:

Okay so I was able to change the behavior by explicitly setting the UIDatePicker date value to the date rounded to the minute interval using the following code:

- (void) handleUIControlEventTouchDown:(id)sender
{
    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    // Set the date picker's minute interval.
    NSInteger minuteInterval  = [sender tag];

    // Setting the date picker's minute interval can change what is selected on
    // the date picker's UI to a wrong date, it does not effect the date
    // picker's date value.
    //
    // For example the date picker's date value is 2:31 and then minute interval
    // is set to 10.  The date value is still 2:31, but 2:10 is selected on the
    // UI, not 2:40 (rounded up) or 2:30 (rounded down).
    //
    // The code that follow's setting the date picker's minute interval
    // addresses fixing the date value (and the selected date on the UI display)
    // .  In the example above both would be 2:30.
    datePicker.minuteInterval = minuteInterval;

    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    // Calculate the proper date value (and the date to be selected on the UI
    // display) by rounding down to the nearest minute interval.
    NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:NSMinuteCalendarUnit fromDate:date];
    NSInteger minutes = [dateComponents minute];
    NSInteger minutesRounded = ( (NSInteger)(minutes / minuteInterval) ) * minuteInterval;
    NSDate *roundedDate = [[NSDate alloc] initWithTimeInterval:60.0 * (minutesRounded - minutes) sinceDate:date];

    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    // Set the date picker's value (and the selected date on the UI display) to
    // the rounded date.
    if ([roundedDate isEqualToDate:datePicker.date])
    {
        // We need to set the date picker's value to something different than
        // the rounded date, because the second call to set the date picker's
        // date with the same value is ignored. Which could be bad since the
        // call above to set the date picker's minute interval can leave the
        // date picker with the wrong selected date (the whole reason why we are
        // doing this).
        NSDate *diffrentDate = [[NSDate alloc] initWithTimeInterval:60 sinceDate:roundedDate];
        datePicker.date = diffrentDate;
        [diffrentDate release];
    }
    datePicker.date = roundedDate;
    [roundedDate release];
}

请注意将UIDatePicker日期设置为两次的部分.弄清楚这一点很有趣.

Pay attention to the part where the UIDatePicker's date is set twice. It was fun figuring that out.

有人知道如何关闭对minuteInterval的呼叫的动画吗?单击5然后10时的幻像滚动有点难看.

Anyone know how to turn the animation off for the call to minuteInterval? The phantom scrolling when clicking 5 then 10 is a little unsightly.

这篇关于UIDatePicker设置分钟间隔时的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆