我试过这段代码:
-(IBAction)btn_strtPeriodclicked:(id)sender{
UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 250, 0, 0)];
// ... populate your date picker and format it as you see fit.
[self.view addSubview:datePicker];
}
pickerView显示但颜色太浅。我的问题是:
答案 0 :(得分:3)
UIDatePicker
。[datePicker removeFromSuperview];
您需要一个日期格式化程序才能从日期获取字符串值。有人喜欢这样:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
用日期设置标签:
self.dateLabel.text = [dateFormatter stringFromDate:datePicker.date];
答案 1 :(得分:0)
-(IBAction)btn_strtPeriodclicked:(id)sender
{
UIDatePicker* datePicker1=[[UIDatePicker alloc]initWithFrame:CGRectMake(0, 290, self.view.frame.size.width, 200)];
datePicker1.datePickerMode = UIDatePickerModeDateAndTime;
[self.view addSubview:datePicker1];
datePicker1.hidden = NO;
[datePicker1 addTarget:self
action:@selector(dateChange:)forControlEvents:UIControlEventValueChanged];
NSDateFormatter *formate=[[NSDateFormatter alloc]init];
[formate setDateFormat:@"dd-MMM-yy hh:mm a "];
//set date too your lable here
timeText1.text=[formate stringFromDate:datePicker1.date];
}
-(void)dateChange:(id)sender
{
NSLog(@"date is %@",datePicker1.date);
NSDateFormatter *formate=[[NSDateFormatter alloc]init];
[formate setDateFormat:@"dd-MMM-yy hh:mm a "];
timeText1.text=[formate stringFromDate:datePicker1.date];
}
答案 2 :(得分:0)
您必须为日期选择器创建视图,并在该视图中添加日期选择器,最初隐藏datepicker视图的框架
首先创建属性并使用IBOutlet连接.h
中的视图和日期选择器@property (strong, nonatomic) IBOutlet UIView *datePickerView;
@property (strong, nonatomic) IBOutlet UIDatePicker *dobPicker;
和viewDIdload()删除datepickerview
-(void)viewDidLoad()
{
[self.datePickerView removeFromSuperview];
}
当您执行操作时显示视图
- (IBAction)showDate
{
UIView *toView;
self.datePickerView.tag =42;
toView = self.datePickerView;
toView.frame = CGRectMake(0, 210, 320, 206);
[self.view addSubview:toView];
}
然后选择日期并将其转换为字符串以在标签中显示
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setDateFormat:@"dd-MMM-yyyy"];
NSDate *date = [self.dobPicker date];
self.label.text = [formatter stringFromDate:birthday];
隐藏在该datepickerview中添加另一个自定义视图和自定义按钮右键隐藏datepicker操作以再次删除视图。您的视图应如下所示,并为完成按钮
创建操作- (IBAction)Done:(id)sender {
for (UIView *view in [self.view subviews])
{
if (view.tag == 42 ) {
[view removeFromSuperview];
}
}
}
1有关详细信息,请参阅此视频see this video