Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
422 views
in Technique[技术] by (71.8m points)

iphone - How to set Minimum time to appear as default in UIDatepicker everytime

In Datepicker i have set Min and Max time where uidatepicker can scroll for example i have set 3:00PM as minimum time and 8:00PM as maximum time , when i run the app i want the datepicker to display the Minimum time not the current time of the device . I have hard coded UIDatepicker in my project havent used in xib file . I have set Min time but when i switch from control.tag ==1 to 2 the minimum time changes and also when i open the app for first time the datepicker doesnt start from minimum time.

UPDATE :

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

    }

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

EDIT:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...