简体   繁体   中英

Row selection in UIPickerView

I want to show the current date on selecting the row of UIDatePickerView on the label, My issue is when I opened my date picker it doesn't show anything on the label on selecting the row but when I move my date picker view, It started updating my label, I am not able to fix it. I want to display the date whenever I touch the date picker view row. I even added TouchUpInside row method also, still, it is not working

Here is my code snippet:

- (IBAction)btn_calendar_event:(id)sender {
    // ADD UIDatePicker and open it
    if (self.picker) {
        [self.toolbar removeFromSuperview];
        [self.picker removeFromSuperview];
    }
    [self hideKeyboard];
    self.picker = [[UIDatePicker alloc] init];
    self.picker.backgroundColor = [UIColor whiteColor];
    [self.picker setValue:[UIColor blackColor] forKey:@"textColor"];

    self.picker.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    self.picker.datePickerMode = UIDatePickerModeDate;
  forControlEvents:UIControlEventValueChanged];

[self.picker addTarget:self action:@selector(dueDateChanged:)forControlEvents:UIControlEventTouchUpInside]; [self.picker addTarget:self action:@selector(dueDateChanged:) forControlEvents:UIControlEventValueChanged]; }

// dueDateChanged method 

-(void) dueDateChanged:(UIDatePicker *)sender {
    NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"YYYY-MM-dd"];

    NSLog(@"Picked the date %@", [dateFormatter stringFromDate:[sender date]]);
   _txtf_birthDate.text = [dateFormatter stringFromDate:[sender date]];

}

// Done button Action 

-(void)onDoneButtonClick {
    [self dueDateChanged :  self.picker];
    [self.toolbar removeFromSuperview];
    [self.picker removeFromSuperview];
}

The problem is when you're initialising your picker your picker is not settings the date to label so it'll only update when dueDateChanged will be triggered so when you initialise your date picker select the date you want to display as a default date and show it in label like

After [self.picker addTarget:self action:@selector(dueDateChanged:) forControlEvents:UIControlEventValueChanged] Simply add a default date to your picker like :-

self.picker.date=[NSDate date]; // add the default initial date
[self dueDateChanged:self.picker];

According to your requirement "If I want to display the current date on selecting row then how to do it, It is now setting default initial date without even selecting the row of date picker view " you can add this line anywhre from where you want to show the date

NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"YYYY-MM-dd"];
_txtf_birthDate.text = [dateFormatter stringFromDate:[NSDate date]];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM