Следующий код отображает странное поведение под iOS 4.3 (возможно, и в другой версии). В этом примере отображается UIDatePicker
, чья дата установлена на 4 Aug 2011 2:31 PM
. UILabel
ниже UIDatePicker
отображает дату для справки. Три UIButtons
ниже, обозначенные 1, 5, 10, устанавливают minuteInterval
на UIDatePicker
.
Нажатие 1 - показывает, что выбранная дата в UIDatePicker
равна 4 Aug 2011 2:31 PM
, а минутный интервал равен 1, что и следовало ожидать.
Нажатие 5 - показывает, что выбранная дата в UIDatePicker
равна 4 Aug 2011 2:35 PM
, а минутный интервал равен 5, что и следовало ожидать (можно утверждать, что время должно округляться, но это не большая проблема ).
Нажатие 10 - показывает, что выбранная дата в UIDatePicker
равна 4 Aug 2011 2:10 PM
, а минутный интервал равен 10. Хорошо, минутный интервал правильный, но выбранное время равно 2:10? Можно было бы ожидать 2:40 (если округлено в большую сторону) или 2:30 (если округлено в меньшую сторону).
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
import "BugDatePickerVC.h"
@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];
}