ios 如何在ios中单击按钮时实现日期选择器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26796302/
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
how to implement date picker on button click in ios?
提问by Daljeet
I am building an iOS app.I am using storyboards to build the screens and i have made a form which contain some fields like Name,Date,min and max.
我正在构建一个 iOS 应用程序。我正在使用故事板来构建屏幕,并且我制作了一个表单,其中包含一些字段,如名称、日期、最小值和最大值。
I am facing an problem that is not able to implement date picker on button click and when user select the date,date picker hide,want to display selected date in a label.
我面临的问题是无法在按钮单击时实现日期选择器,当用户选择日期时,日期选择器隐藏,想要在标签中显示选定的日期。
I googled but could not find a good tutorial or library. I want to show only days and date in picker.
我用谷歌搜索但找不到好的教程或库。我只想在选择器中显示日期和日期。
回答by iosDev
-(IBAction)datePickerBtnAction:(id)sender
{
datePicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 0,10, 50)];
datePicker.datePickerMode = UIDatePickerModeDate;
datePicker.hidden = NO;
datePicker.date = [NSDate date];
[datePicker addTarget:self action:@selector(labelTitle:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:datePicker];
rightBtn = [[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(save:)];
self.navigationItem.rightBarButtonItem = rightBtn;
}
-(void)labelTitle:(id)sender
{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
dateFormat.dateStyle = NSDateFormatterMediumStyle;
[dateFormat setDateFormat:@"MM/dd/yyyy"];
NSString *str = [NSString stringWithFormat:@"%@",[dateFormat stringFromDate:datePicker.date]];
//assign text to label
label.text=str;
}
-(void)save:(id)sender
{
self.navigationItem.rightBarButtonItem = nil;
[datePicker removeFromSuperview];
}
回答by Mishal Awan
Try this:
尝试这个:
- (IBAction)date:(id)sender {
datepicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 250, 325, 300)];
datepicker.datePickerMode = UIDatePickerModeDate;
datepicker.hidden = NO;
datepicker.date = [NSDate date];
[datepicker addTarget:self
action:@selector(labelChange:)
forControlEvents:UIControlEventValueChanged];
[self.view addSubview:datepicker]; //this can set value of selected date to your label change according to your condition
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"M-d-yyyy"]; // from here u can change format..
_selectedDate.text = [df stringFromDate:datepicker.date];
}
- (void)labelChange:(id)sender{
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"M-d-yyyy"];
_selectedDate.text = [NSString stringWithFormat:@"%@",
[df stringFromDate:datepicker.date]];
[datepicker removeFromSuperview];
}
回答by Jay Gajjar
You can use various controls form this
您可以使用各种控件形成此
https://www.cocoacontrols.com/search?utf8=%E2%9C%93&q=datepicker
https://www.cocoacontrols.com/search?utf8=%E2%9C%93&q=datepicker
回答by Chathurka
Add hidden textfield view and configure the date picker.
添加隐藏的文本字段视图并配置日期选择器。
[self.xTextFeild setInputView:_datePicker];
Then When ever you want call below code.
然后当你想调用下面的代码时。
[self.xTextFeild becomeFirstResponder];
`
`