user594161
user594161

Reputation:

Set UIButton's Text To Date

I have a UIButton that when clicked brings up a UIDatePicker that will set it's date.

Everything is set up with the animation in and out, etc.

What I need help with:

Setting the button's text label to the day's date by default. And then also setting it to the newly selected date after the UIDatePicker is dismissed.

 - (IBAction)dateButtonPressed
{
    [dateView setFrame:CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, self.view.frame.size.height)];
    [self.view addSubview:dateView];

    [UIView animateWithDuration:.5 animations:^{
        [dateView setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    }];
}

- (IBAction)dismssPicker
{
    [dateView setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

    [UIView animateWithDuration:.5 animations:^{
        [dateView setFrame:CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, self.view.frame.size.height)];
    } completion:^(BOOL finished){ [dateView removeFromSuperview]; }];
}

Upvotes: 2

Views: 389

Answers (3)

Inder Kumar Rathore
Inder Kumar Rathore

Reputation: 39988

First change you method to this. (I'm assuming you have connected this to UIDatePicker)

 -(IBAction)dismssPicker:(UIDatePicker*)datePicker
{

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    NSDate *date = datePicker.date;
    [dateFormatter setDateFormat:@"MMM dd, yyyy"]; 
    NSString *dateString = [dateFormatter stringFromDate:date]; 
//set it now


    [dateView setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

    [UIView animateWithDuration:.5 animations:^{
        [dateView setFrame:CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, self.view.frame.size.height)];
    } completion:^(BOOL finished){ [dateView removeFromSuperview]; }];
}

For your next problem, make as NSDate *prvDate; in your .h file

-(IBAction)changeDate:(id)sender{
    prvDate = yourCurrentDate;  //save your previous date 
    yourCurrentDate = datePicker.date;
//set the button's label here
}

 -(IBAction)cancel:(id)sender{
    yourCurrentDate = prvDate;
//set the button's label here
}

Upvotes: 0

reflog
reflog

Reputation: 7645

you probably want something like:

[button setTitle:[[NSDate date] descriptionWithCalendarFormat:@"%Y-%m-%d"
                                    timezone:nil
                                      locale:nil] forState:UIControlStateNormal];

Upvotes: 1

Inder Kumar Rathore
Inder Kumar Rathore

Reputation: 39988

- (void)setTitle:(NSString *)title forState:(UIControlState)state

to change the text of the button.

like

[button setTitle:[date description] forState:UIControlStateNormal]

Upvotes: 1

Related Questions