UiDatePicker Xcode 6未在标签中显示当前日期,未完成或取消按钮

时间:2015-02-04 08:43:09

标签: ios ios8 datepicker xcode6 uilabel

伙计我似乎无法弄清楚当我实现了不允许选择当前日期的日期选择器的时候。当弹出日期选择器时,我也在这里按照许多答案显示完成按钮。

请默认帮助我完成按钮并将标签设置为当前日期。

这是我的屏幕截图enter image description here

- (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];

}

2 个答案:

答案 0 :(得分:0)

尝试使用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];
}

这样,每次点击textField都会出现日期选择器,或者调用becomeFirstResponder

答案 1 :(得分:0)

我通过这个有用的教程找到了我的问题的答案。以下是有人可以使用的代码片段,如果他们想要取消日期选择器,则从片段下方的链接获取完成按钮以获取更多信息和作者信用。

/*
 * 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];
}

iOS – How To Make DatePicker