UIDatePickerModeTime 限制时间选择或设置时间范围
UIDatePickerModeTime limiting time selection or set time range
我写了下面的一段代码来带来 pickerView 敌人选择时间。现在运行良好,我想知道如何设置上午 9 点到下午 5 点之间的时间范围。请有人帮助我实现限制时间选择在上午 9 点至下午 5 点之间的要求。
@interface CleaningDetails ()<UITextFieldDelegate>
{
IBOutlet TextFieldValidator *cleanTime;
UIDatePicker *time;
}
@implementation
time = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
time.datePickerMode = UIDatePickerModeTime;
time.minuteInterval = 30;
// set change the inputView (default is keyboard) to UIPickerView
[self.cleanTime setInputView:time];
// add a toolbar with Cancel & Done button
UIToolbar *toolBar1 = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
toolBar1.barStyle = UIBarStyleBlackOpaque;
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(showSelectedTime)];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelTouched:)];
// the middle button is to make the Done button align to right
[toolBar1 setItems:[NSArray arrayWithObjects:cancelButton, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], doneButton, nil]];
[self.cleanTime setInputAccessoryView:toolBar1];
我对这个编程真的很陌生,objective-C 特别是任何人都可以向我建议一些资源或任何想法来完成这个要求。
UIDatePicker 只支持最小和最大日期,不支持时间。但您将使用时间间隔设置时间,使用下面的代码并设置您的时间,
NSDate *minDate = [NSDate new];
// One hour from now
NSDate *maxDate = [[NSDate alloc] initWithTimeIntervalSinceNow:60*60];
UIDatePicker *picker = [UIDatePicker new];
[picker setDatePickerMode:UIDatePickerModeDateAndTime];
[picker setMinimumDate:minDate];
[picker setMaximumDate:maxDate];
希望对您有所帮助
试试这个代码,
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
startTime.text = [NSString stringWithFormat:@"%@",[self beginningOfDay:[NSDate new]]];
endTime.text = [NSString stringWithFormat:@"%@",[self endOfDay:[NSDate new]]];
}
-(NSDate *)beginningOfDay:(NSDate *)date
{
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:( NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:date];
[components setHour:9];
[components setMinute:0];
[components setSecond:0];
return [cal dateFromComponents:components];
}
-(NSDate *)endOfDay:(NSDate *)date
{
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:date];
[components setHour:17];
[components setMinute:0];
[components setSecond:0];
return [cal dateFromComponents:components];
}
你会得到return时间,然后将UTC时间转换成本地时间,
希望对您有所帮助。
我写了下面的一段代码来带来 pickerView 敌人选择时间。现在运行良好,我想知道如何设置上午 9 点到下午 5 点之间的时间范围。请有人帮助我实现限制时间选择在上午 9 点至下午 5 点之间的要求。
@interface CleaningDetails ()<UITextFieldDelegate>
{
IBOutlet TextFieldValidator *cleanTime;
UIDatePicker *time;
}
@implementation
time = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
time.datePickerMode = UIDatePickerModeTime;
time.minuteInterval = 30;
// set change the inputView (default is keyboard) to UIPickerView
[self.cleanTime setInputView:time];
// add a toolbar with Cancel & Done button
UIToolbar *toolBar1 = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
toolBar1.barStyle = UIBarStyleBlackOpaque;
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(showSelectedTime)];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelTouched:)];
// the middle button is to make the Done button align to right
[toolBar1 setItems:[NSArray arrayWithObjects:cancelButton, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], doneButton, nil]];
[self.cleanTime setInputAccessoryView:toolBar1];
我对这个编程真的很陌生,objective-C 特别是任何人都可以向我建议一些资源或任何想法来完成这个要求。
UIDatePicker 只支持最小和最大日期,不支持时间。但您将使用时间间隔设置时间,使用下面的代码并设置您的时间,
NSDate *minDate = [NSDate new];
// One hour from now
NSDate *maxDate = [[NSDate alloc] initWithTimeIntervalSinceNow:60*60];
UIDatePicker *picker = [UIDatePicker new];
[picker setDatePickerMode:UIDatePickerModeDateAndTime];
[picker setMinimumDate:minDate];
[picker setMaximumDate:maxDate];
希望对您有所帮助
试试这个代码,
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
startTime.text = [NSString stringWithFormat:@"%@",[self beginningOfDay:[NSDate new]]];
endTime.text = [NSString stringWithFormat:@"%@",[self endOfDay:[NSDate new]]];
}
-(NSDate *)beginningOfDay:(NSDate *)date
{
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:( NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:date];
[components setHour:9];
[components setMinute:0];
[components setSecond:0];
return [cal dateFromComponents:components];
}
-(NSDate *)endOfDay:(NSDate *)date
{
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:date];
[components setHour:17];
[components setMinute:0];
[components setSecond:0];
return [cal dateFromComponents:components];
}
你会得到return时间,然后将UTC时间转换成本地时间,
希望对您有所帮助。