how to implement date picker on button click in ios?

后端 未结 4 1614
忘了有多久
忘了有多久 2021-02-20 04:23

I am building an iOS app.I am using storyboards to build the screens and i have made a form which contain some fields like Name,Date,min and max.

I am facing an problem

4条回答
  •  庸人自扰
    2021-02-20 04:38

    Try this:

    - (IBAction)date:(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];
        [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.. 
        _selectedDate.text = [df stringFromDate:datepicker.date];
    }
    
    - (void)labelChange:(id)sender{
        NSDateFormatter *df = [[NSDateFormatter alloc] init];
        [df setDateFormat:@"M-d-yyyy"];
        _selectedDate.text = [NSString stringWithFormat:@"%@",
                              [df stringFromDate:datepicker.date]];
        [datepicker removeFromSuperview];
    }
    

提交回复
热议问题