作者热门文章
- objective-c - iOS 5 : Can you override UIAppearance customisations in specific classes?
- iphone - 如何将 CGFontRef 转换为 UIFont?
- ios - 以编程方式关闭标记的信息窗口 google maps iOS
- ios - Xcode 5 - 尝试验证存档时出现 "No application records were found"
我正在构建一个 iOS 应用程序。我正在使用 Storyboard来构建屏幕,我制作了一个包含名称、日期、最小值和最大值等字段的表单。
我遇到的问题是无法在单击按钮时实现日期选择器,当用户选择日期时,日期选择器隐藏,想要在标签中显示所选日期。
我用谷歌搜索但找不到好的教程或库。我只想在选择器中显示天数和日期。
最佳答案
-(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 - 如何在 ios 中点击按钮时实现日期选择器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26796302/
我是一名优秀的程序员,十分优秀!