1

在 Datepicker 中,我设置了 uidatepicker 可以滚动的最小和最大时间,例如,我将 3:00PM 设置为最小时间,将 8:00PM 设置为最大时间,当我运行应用程序时,我希望 datepicker 显示最小时间而不是当前时间的设备。我在我的项目中硬编码了 UIDatepicker,并没有在 xib 文件中使用。我已经设置了最小时间,但是当我从 control.tag ==1 切换到 2 时,最小时间会发生变化,而且当我第一次打开应用程序时,日期选择器不会从最小时间开始。

更新 :

-(IBAction)notificationButtonChanged:(UIButton *)control
{

    if (control.tag == 1)
    {
        self.selectedButton = control.tag;

        [userTimePicker reloadInputViews];

        [components setHour:7];;
        [components setMinute:00];

        NSDate *date1 = [gregorian dateFromComponents:components];

        [components setHour:11];
        [components setMinute:00];

        NSDate *date2 = [gregorian dateFromComponents:components];

        [userTimePicker setDatePickerMode:UIDatePickerModeTime];

        [userTimePicker setMinimumDate:date1];
        [userTimePicker setMaximumDate:date2];

    }

    if (control.tag == 2)
    {
        self.selectedButton = control.tag;

        [userTimePicker reloadInputViews];

        [components setHour:12];
        [components setMinute:00];

        NSDate *date1 = [gregorian dateFromComponents:components];

        [components setHour:17];
        [components setMinute:00];

        NSDate *date2 = [gregorian dateFromComponents:components];

        [userTimePicker setDatePickerMode:UIDatePickerModeTime];

        [userTimePicker setMinimumDate:date1];
        [userTimePicker setMaximumDate:date2];

    }

}
4

5 回答 5

10

编辑:

int startHour = 7;
int endHour = 11;

NSDate *date1 = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
NSDateComponents *components = [gregorian components: NSUIntegerMax fromDate: date1];
[components setHour: startHour];
[components setMinute: 0];
[components setSecond: 0];
NSDate *startDate = [gregorian dateFromComponents: components];

[components setHour: endHour];
[components setMinute: 0];
[components setSecond: 0];
NSDate *endDate = [gregorian dateFromComponents: components];

[userTimePicker setDatePickerMode:UIDatePickerModeTime];
[userTimePicker setMinimumDate:startDate];
[userTimePicker setMaximumDate:endDate];
[userTimePicker setDate:startDate animated:YES];
[userTimePicker reloadInputViews];
于 2013-05-01T16:52:02.880 回答
4

对于 Swift 版本:

let startHour: Int = 6
let endHour: Int = 23
let date1: NSDate = NSDate()
let gregorian: NSCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
let components: NSDateComponents = gregorian.components(([.Day, .Month, .Year]), fromDate: date1)
components.hour = startHour
components.minute = 0
components.second = 0
let startDate: NSDate = gregorian.dateFromComponents(components)!
components.hour = endHour
components.minute = 0
components.second = 0
let endDate: NSDate = gregorian.dateFromComponents(components)!
datePicker.datePickerMode = .Time
datePicker.minimumDate = startDate
datePicker.maximumDate = endDate
datePicker.setDate(startDate, animated: true)
datePicker.reloadInputViews()
于 2016-06-28T09:08:47.643 回答
3

为 Swift 3 更新:

        let startHour: Int = 6
        let endHour: Int = 23
        let date1: NSDate = NSDate()
        let gregorian: NSCalendar = NSCalendar(calendarIdentifier: NSCalendar.Identifier.gregorian)!

        let components: NSDateComponents = gregorian.components(([.day, .month, .year]), from: date1 as Date) as NSDateComponents
        components.hour = startHour
        components.minute = 0
        components.second = 0
        let startDate: NSDate = gregorian.date(from: components as DateComponents)! as NSDate

        components.hour = endHour
        components.minute = 0
        components.second = 0
        let endDate: NSDate = gregorian.date(from: components as DateComponents)! as NSDate

        timePicker.datePickerMode = .time
        timePicker.minimumDate = startDate as Date
        timePicker.maximumDate = endDate as Date
        timePicker.setDate(startDate as Date, animated: true)
        timePicker.reloadInputViews()
        timePicker.frame = CGRect(x: 0.0, y: (self.view.frame.height/2 + 60), width: self.view.frame.width, height: 150.0)
        timePicker.backgroundColor = UIColor.white
//        timePicker.reloadInputViews()
        self.view.addSubview(timePicker)
        timePicker.addTarget(self, action: #selector(RoundTripViewController.startTimeDiveChanged), for: UIControlEvents.valueChanged)
于 2017-08-14T10:48:00.560 回答
2

UIDatePicker 类有一个setDate:animated:方法,您可以使用该方法将日期设置为日期选择器实例上所需的任何日期/时间。

更多信息:http: //developer.apple.com/library/ios/documentation/uikit/reference/UIDatePicker_Class/Reference/UIDatePicker.html#//apple_ref/doc/uid/TP40006806-CH3-SW1

于 2013-05-01T16:48:55.330 回答
1
datePickerObj = [[UIDatePicker alloc] initWithFrame:CGRectMake(10, 100, 300, 160)];
datePickerObj.datePickerMode = UIDatePickerModeTime;
datePickerObj.backgroundColor = [UIColor clearColor];
[datePickerObj addTarget:self action:@selector(onDatePickerValueChanged:) forControlEvents:UIControlEventValueChanged];

将字符串传递到方法和格式中...

-(void)Set_MinimumDate_ToDatePicker:(NSString*)Time //  "start_time" = "3:00PM";
{
    NSDateFormatter *dformat = [[NSDateFormatter alloc]init];
    [dformat setDateFormat:@"hh:mma"];

    [datePickerObj setMinimumDate:[dformat dateFromString:Time]];
    [datePickerObj setDate:[dformat dateFromString:Time] animated:YES];
    [datePickerObj reloadInputViews];
}

在更改选择器时,它会将时间转换为 UTC+00:00,但它会显示您所在时区的时间,例如。印度,它将 UTC+05:30 添加到时间。如果有任何 TimeZone 问题,请将 TimeZone 设置为NSDateFormatter

-(void)onDatePickerValueChanged:(UIDatePicker *)datePicker
{
    NSDateFormatter *dformat = [[NSDateFormatter alloc]init];
    [dformat setDateFormat:@"hh:mm a"];

    NSLog(@"%@",datePicker.date); //  2000-01-01 11:30:00 +0000
    [newStartBtn setTitle:[dformat stringFromDate:datePicker.date] forState:UIControlStateNormal]; 
    //"05:00 PM";
 }
于 2015-06-16T10:47:02.160 回答