如何在ios上点击按钮实现日期选择器? [英] how to implement date picker on button click in ios?
本文介绍了如何在ios上点击按钮实现日期选择器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在构建一个iOS应用程序。我正在使用故事板来构建屏幕,我已经创建了一个包含名称,日期,分钟和最大值等字段的表单。
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.
我遇到的问题是无法在按钮点击时实现日期选择器,当用户选择日期,日期选择器隐藏时,想要在标签中显示所选日期。
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.
推荐答案
-(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];
}
这篇关于如何在ios上点击按钮实现日期选择器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文