UiDatePicker Xcode 6 没有在标签中显示当前日期并且没有完成或取消按钮
UiDatePicker Xcode 6 not showing current date in label and no done or cancel button
伙计们,我似乎无法弄清楚为什么当我实现了日期选择器时它不允许选择当前日期。当日期选择器 pops-up.
时,我也按照这里的许多答案显示完成按钮
请帮助我完成按钮并将标签默认设置为当前日期。
这是我的截图
- (IBAction)btnDate:(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];
datepicker.backgroundColor = [UIColor whiteColor];
[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..
lblDate.text=[df stringFromDate:datepicker.date];
}
- (void)LabelChange:(id)sender{
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"M-d-yyyy"];
lblDate.text = [NSString stringWithFormat:@"%@",
[df stringFromDate:datepicker.date]];
[datepicker removeFromSuperview];
}
尝试在下一个代码中使用 UITextField 而不是 UILabel:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
// Create date picker
UIDatePicker * datePicker = [UIDatePicker new];
// Create and configure toolbar
UIToolbar* toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 44)];
UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:@selector(doneAction:)];
UIBarButtonItem* cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self
action:@selector(cancelAction:)];
UIBarButtonItem* flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
[toolbar setItems:@[cancelButton, flexibleSpace, doneButton]];
// Set date picker as input view
self.textField.inputView = datePicker;
// Set toolbar as accessory view
self.textField.inputAccessoryView = toolbar;
}
- (void)doneAction:(id)sender
{
UIDatePicker* datePicker = (UIDatePicker*)[self.textField inputView];
self.textField.text = datePicker.date.description;
[self.textField resignFirstResponder];
}
- (void)cancelAction:(id)sender
{
[self.textField resignFirstResponder];
}
这样,每次点击文本字段或调用 becomeFirstResponder
时都会出现日期选择器
我通过这个有用的教程找到了问题的答案。下面是一段代码,如果有人想要带有取消和完成按钮的日期选择器,可以使用代码段下面的 link 来获取更多信息并感谢作者。
/*
* Change the date.
*/
- (void)changePickUpDate:(UIDatePicker *)sender {
NSString *formatString = [NSDateFormatter dateFormatFromTemplate:@"yyyyMMdd" options:0 locale:[NSLocale currentLocale]];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:formatString];
NSString *todayString = [dateFormatter stringFromDate:sender.date];
NSLog(@"todayString: %@", todayString);
DateLabel.text =[[NSString stringWithFormat:@"%@", todayString]init];
//NSLog(@"New Date: %@", sender.date);
}
/*
* Releases the background, datepicker and toolbar.
*/
- (void)removeViews:(id)object {
/*
* Releases the background view.
*/
[[self.view viewWithTag:9] removeFromSuperview];
/*
* Releases the datepicker.
*/
[[self.view viewWithTag:10] removeFromSuperview];
/*
* Releases the toolbar.
*/
[[self.view viewWithTag:11] removeFromSuperview];
}
/*
* Hides the datapicker.
*/
- (void)dismissDatePicker:(id)sender {
/*
* Create a variable with the target position and size for hiding the toolbar.
*/
CGRect toolbarTargetFrame = CGRectMake(0, self.view.bounds.size.height, 320, 44);
/*
* Create a variable with the target position and size for hiding the datepicker.
*/
CGRect datePickerTargetFrame = CGRectMake(0, self.view.bounds.size.height + 44, 320, 216);
/*
* Send start animation message to UIView.
*/
[UIView beginAnimations:@"MoveOut" context:nil];
/*
* Set the transparency for hiding the background view.
*/
[self.view viewWithTag:9].alpha = 0;
/*
* Set the target position for hiding the datepicker.
*/
[self.view viewWithTag:10].frame = datePickerTargetFrame;
/*
* Set the target position for hiding the toolbar.
*/
[self.view viewWithTag:11].frame = toolbarTargetFrame;
/*
* The method setAnimationDelegate enables knowledge on start and end of the animation.
*/
[UIView setAnimationDelegate:self];
/*
* Calls the removeViews method at the end of the animation.
*/
[UIView setAnimationDidStopSelector:@selector(removeViews:)];
/*
* Commits the animation thread for execution.
*/
[UIView commitAnimations];
}
/*
* Sets up and shows the datepicker.
*/
- (IBAction)callPickUpDP {
/*
* If view with tag exists ignore the rest of the code and exit.
*/
if([self.view viewWithTag:9]) {
return;
}
/*
* Create a variable with the target position and size for showing the toolbar.
*/
CGRect toolbarTargetFrame = CGRectMake(0, self.view.bounds.size.height - 216 - 44, 320, 44);
/*
* Create a variable with the target position and size for showing the datepicker.
*/
CGRect datePickerTargetFrame = CGRectMake(0, self.view.bounds.size.height - 216, 320, 216);
/*
* Instantiate a UIView with the frame size of the existing view.
*/
UIView *darkView = [[UIView alloc] initWithFrame:self.view.bounds];
/*
* Set the transparency.
*/
darkView.alpha = 0;
/*
* Set the background color.
*/
darkView.backgroundColor = [UIColor blackColor];
/*
* Set a tag for the UIView instance to reference it by tag.
*/
darkView.tag = 9;
/*
* Setup a tap gesture listener that calls the dismissDatePicker method on tap.
*/
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissDatePicker:)];
/*
* Add the tap gesture listener to the UIView.
*/
[darkView addGestureRecognizer:tapGesture];
/*
* Adds the subview on top of all the other subviews.
*/
[self.view addSubview:darkView];
/*
* Instantiate a UIDatePicker and set the initial frame with the datepicker outside of the view.
*/
UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height+44, 320, 216)];
/*
* Set a tag for the UIDatePicker instance to reference it by tag.
*/
datePicker.alpha = 1;
datePicker.tag = 10;
datePicker.backgroundColor = [UIColor whiteColor];
datePicker.datePickerMode = UIDatePickerModeDate;
/*
* Add a listener that listens for changing values in the datepicker which then calls the change date method.
*/
[datePicker addTarget:self action:@selector(changePickUpDate:) forControlEvents:UIControlEventValueChanged];
/*
* Adds the subview on top of all the other subviews.
*/
[self.view addSubview:datePicker];
/*
* Instantiate a UIToolbar and set the initial frame with the toolbar outside of the view.
*/
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height, 320, 44)];
/*
* Set a tag for the UIToolbar instance to reference it by tag.
*/
toolBar.tag = 11;
/*
* Set a style for the UIToolbar.
*/
toolBar.barStyle = UIBarStyleBlackTranslucent;
/*
* Instantiate a spacer UIBarButtonItem.
*/
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
/*
* Instantiate a done UIBarButtonItem on click this will call the dismissDatePicker method.
*/
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissDatePicker:)];
/*
* Set the created UIBarButtonItems to the toolbar.
*/
[toolBar setItems:[NSArray arrayWithObjects:spacer, doneButton, nil]];
/*
* Adds the subview on top of all the other subviews.
*/
[self.view addSubview:toolBar];
/*
* Start animation.
*/
[UIView beginAnimations:@"MoveIn" context:nil];
/*
* Set the target position for showing the toolbar.
*/
toolBar.frame = toolbarTargetFrame;
/*
* Set the target position for showing the datepicker.
*/
datePicker.frame = datePickerTargetFrame;
/*
* Set the transparency for showing background view.
*/
darkView.alpha = 0.5;
/*
* Commits the animation thread for execution.
*/
[UIView commitAnimations];
}
伙计们,我似乎无法弄清楚为什么当我实现了日期选择器时它不允许选择当前日期。当日期选择器 pops-up.
时,我也按照这里的许多答案显示完成按钮请帮助我完成按钮并将标签默认设置为当前日期。
这是我的截图
- (IBAction)btnDate:(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];
datepicker.backgroundColor = [UIColor whiteColor];
[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..
lblDate.text=[df stringFromDate:datepicker.date];
}
- (void)LabelChange:(id)sender{
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"M-d-yyyy"];
lblDate.text = [NSString stringWithFormat:@"%@",
[df stringFromDate:datepicker.date]];
[datepicker removeFromSuperview];
}
尝试在下一个代码中使用 UITextField 而不是 UILabel:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
// Create date picker
UIDatePicker * datePicker = [UIDatePicker new];
// Create and configure toolbar
UIToolbar* toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 44)];
UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:@selector(doneAction:)];
UIBarButtonItem* cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self
action:@selector(cancelAction:)];
UIBarButtonItem* flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
[toolbar setItems:@[cancelButton, flexibleSpace, doneButton]];
// Set date picker as input view
self.textField.inputView = datePicker;
// Set toolbar as accessory view
self.textField.inputAccessoryView = toolbar;
}
- (void)doneAction:(id)sender
{
UIDatePicker* datePicker = (UIDatePicker*)[self.textField inputView];
self.textField.text = datePicker.date.description;
[self.textField resignFirstResponder];
}
- (void)cancelAction:(id)sender
{
[self.textField resignFirstResponder];
}
这样,每次点击文本字段或调用 becomeFirstResponder
我通过这个有用的教程找到了问题的答案。下面是一段代码,如果有人想要带有取消和完成按钮的日期选择器,可以使用代码段下面的 link 来获取更多信息并感谢作者。
/*
* Change the date.
*/
- (void)changePickUpDate:(UIDatePicker *)sender {
NSString *formatString = [NSDateFormatter dateFormatFromTemplate:@"yyyyMMdd" options:0 locale:[NSLocale currentLocale]];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:formatString];
NSString *todayString = [dateFormatter stringFromDate:sender.date];
NSLog(@"todayString: %@", todayString);
DateLabel.text =[[NSString stringWithFormat:@"%@", todayString]init];
//NSLog(@"New Date: %@", sender.date);
}
/*
* Releases the background, datepicker and toolbar.
*/
- (void)removeViews:(id)object {
/*
* Releases the background view.
*/
[[self.view viewWithTag:9] removeFromSuperview];
/*
* Releases the datepicker.
*/
[[self.view viewWithTag:10] removeFromSuperview];
/*
* Releases the toolbar.
*/
[[self.view viewWithTag:11] removeFromSuperview];
}
/*
* Hides the datapicker.
*/
- (void)dismissDatePicker:(id)sender {
/*
* Create a variable with the target position and size for hiding the toolbar.
*/
CGRect toolbarTargetFrame = CGRectMake(0, self.view.bounds.size.height, 320, 44);
/*
* Create a variable with the target position and size for hiding the datepicker.
*/
CGRect datePickerTargetFrame = CGRectMake(0, self.view.bounds.size.height + 44, 320, 216);
/*
* Send start animation message to UIView.
*/
[UIView beginAnimations:@"MoveOut" context:nil];
/*
* Set the transparency for hiding the background view.
*/
[self.view viewWithTag:9].alpha = 0;
/*
* Set the target position for hiding the datepicker.
*/
[self.view viewWithTag:10].frame = datePickerTargetFrame;
/*
* Set the target position for hiding the toolbar.
*/
[self.view viewWithTag:11].frame = toolbarTargetFrame;
/*
* The method setAnimationDelegate enables knowledge on start and end of the animation.
*/
[UIView setAnimationDelegate:self];
/*
* Calls the removeViews method at the end of the animation.
*/
[UIView setAnimationDidStopSelector:@selector(removeViews:)];
/*
* Commits the animation thread for execution.
*/
[UIView commitAnimations];
}
/*
* Sets up and shows the datepicker.
*/
- (IBAction)callPickUpDP {
/*
* If view with tag exists ignore the rest of the code and exit.
*/
if([self.view viewWithTag:9]) {
return;
}
/*
* Create a variable with the target position and size for showing the toolbar.
*/
CGRect toolbarTargetFrame = CGRectMake(0, self.view.bounds.size.height - 216 - 44, 320, 44);
/*
* Create a variable with the target position and size for showing the datepicker.
*/
CGRect datePickerTargetFrame = CGRectMake(0, self.view.bounds.size.height - 216, 320, 216);
/*
* Instantiate a UIView with the frame size of the existing view.
*/
UIView *darkView = [[UIView alloc] initWithFrame:self.view.bounds];
/*
* Set the transparency.
*/
darkView.alpha = 0;
/*
* Set the background color.
*/
darkView.backgroundColor = [UIColor blackColor];
/*
* Set a tag for the UIView instance to reference it by tag.
*/
darkView.tag = 9;
/*
* Setup a tap gesture listener that calls the dismissDatePicker method on tap.
*/
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissDatePicker:)];
/*
* Add the tap gesture listener to the UIView.
*/
[darkView addGestureRecognizer:tapGesture];
/*
* Adds the subview on top of all the other subviews.
*/
[self.view addSubview:darkView];
/*
* Instantiate a UIDatePicker and set the initial frame with the datepicker outside of the view.
*/
UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height+44, 320, 216)];
/*
* Set a tag for the UIDatePicker instance to reference it by tag.
*/
datePicker.alpha = 1;
datePicker.tag = 10;
datePicker.backgroundColor = [UIColor whiteColor];
datePicker.datePickerMode = UIDatePickerModeDate;
/*
* Add a listener that listens for changing values in the datepicker which then calls the change date method.
*/
[datePicker addTarget:self action:@selector(changePickUpDate:) forControlEvents:UIControlEventValueChanged];
/*
* Adds the subview on top of all the other subviews.
*/
[self.view addSubview:datePicker];
/*
* Instantiate a UIToolbar and set the initial frame with the toolbar outside of the view.
*/
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height, 320, 44)];
/*
* Set a tag for the UIToolbar instance to reference it by tag.
*/
toolBar.tag = 11;
/*
* Set a style for the UIToolbar.
*/
toolBar.barStyle = UIBarStyleBlackTranslucent;
/*
* Instantiate a spacer UIBarButtonItem.
*/
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
/*
* Instantiate a done UIBarButtonItem on click this will call the dismissDatePicker method.
*/
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissDatePicker:)];
/*
* Set the created UIBarButtonItems to the toolbar.
*/
[toolBar setItems:[NSArray arrayWithObjects:spacer, doneButton, nil]];
/*
* Adds the subview on top of all the other subviews.
*/
[self.view addSubview:toolBar];
/*
* Start animation.
*/
[UIView beginAnimations:@"MoveIn" context:nil];
/*
* Set the target position for showing the toolbar.
*/
toolBar.frame = toolbarTargetFrame;
/*
* Set the target position for showing the datepicker.
*/
datePicker.frame = datePickerTargetFrame;
/*
* Set the transparency for showing background view.
*/
darkView.alpha = 0.5;
/*
* Commits the animation thread for execution.
*/
[UIView commitAnimations];
}