简体   繁体   中英

UIDatePicker first value set is not default to read

I have set the initial date in the datepicker in my application. If I change the value and select it picks it up, however when selecting the value without changing the spinner the value it shows is todays date.

I have been trying to use the [UIDatePicker setDate:] method, however I am having problems with it, it wont set. This is my code for 'selecting' the date (action assigned to a button):

- (IBAction)backToMap:(id)sender {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    FirstViewController *fvc = [storyboard instantiateViewControllerWithIdentifier:@"FirstViewController"];
    fvc.modalTransitionStyle = UIModalTransitionStyleCoverVertical;

    datePicked = [datePicker date];

    self.firstViewData = fvc;

    fvc.passedData = datePicked;

    // if isPickerType = 1 then it is date and time
    // if isPickerType = 2 then it is just date
    fvc.datePicker = isPickerType;

    [self presentViewController:fvc animated:YES completion:nil];
}

When you do:

FirstViewController *fvc = [storyboard instantiateViewControllerWithIdentifier:@"FirstViewController"];

You create a new controller, so it has a new date picker. So, the date it had is the default date.

You should be using the date picker in an existing view controller, not creating a new one if this is the point at which you're trying to get the user chosen date.

If this is really where you're creating the controller then you could set the date you want where you currently try to get the 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