xcode 显示 18 年前的日期选择器并在 ios 中锁定 ui 日期选择器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12853795/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
showing datepicker 18 years back and lock the ui datepicker in ios
提问by sabir
In my project i need to show the date picker 18 back and i need to lock the 80 years backs so how can i show those date in datepicker can any one help me to find here here am adding code my code i printed in log but i need to display on my datepicker so how can i display
在我的项目中,我需要向后显示日期选择器 18 并且我需要锁定 80 年,所以我如何在日期选择器中显示这些日期有人可以帮我在这里找到我添加代码我在日志中打印的代码但是我需要在我的日期选择器上显示,所以我如何显示
NSCalendar * gregorian = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
NSDate * currentDate = [NSDate date];
NSDateComponents * comps = [[NSDateComponents alloc] init];
[comps setYear: -18];
NSDate * minDate = [gregorian dateByAddingComponents: comps toDate: currentDate options: 0];
[comps setYear: -100];
NSDate * maxDate = [gregorian dateByAddingComponents: comps toDate: currentDate options: 0];
[comps release];
self.datePickerView.minimumDate = minDate;
self.datePickerView.maximumDate = maxDate;
NSLog(@"minDate %@", minDate);
NSLog(@"maxDate%@", maxDate);
回答by abhishekkharwar
Hi first of all minimum date must be 100 year back date and max date must be 18 year back date and then set pickerView's date to a date between range of dates so your final code will be looks like
嗨,首先最小日期必须是 100 年前的日期,最大日期必须是 18 年前的日期,然后将 pickerView 的日期设置为日期范围之间的日期,因此您的最终代码将如下所示
NSCalendar * gregorian = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
NSDate * currentDate = [NSDate date];
NSDateComponents * comps = [[NSDateComponents alloc] init];
[comps setYear: -18];
NSDate * maxDate = [gregorian dateByAddingComponents: comps toDate: currentDate options: 0];
[comps setYear: -100];
NSDate * minDate = [gregorian dateByAddingComponents: comps toDate: currentDate options: 0];
[comps release];
self.datePickerView.minimumDate = minDate;
self.datePickerView.maximumDate = maxDate;
self.datePickerView.date = minDate;
Hopefully this will be helpful for you.
希望这对你有帮助。
回答by abhishekkharwar
Here is viewDidLoad method of view controller class
这是视图控制器类的 viewDidLoad 方法
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSCalendar * gregorian = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
NSDate * currentDate = [NSDate date];
NSDateComponents * comps = [[NSDateComponents alloc] init];
[comps setYear: -18];
NSDate * maxDate = [gregorian dateByAddingComponents: comps toDate: currentDate options: 0];
[comps setYear: -100];
NSDate * minDate = [gregorian dateByAddingComponents: comps toDate: currentDate options: 0];
[comps release];
UIDatePicker *pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, 320, 300)];
[pickerView setMinimumDate:minDate];
[pickerView setMaximumDate:maxDate];
[pickerView setDate:minDate];
[[self view] addSubview:pickerView];
}
My Output is
我的输出是
can you please share your output as screenshot ?
你能把你的输出分享为截图吗?