how to implement date picker on button click in ios?

后端 未结 4 1589
忘了有多久
忘了有多久 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:26

    -(IBAction)datePickerBtnAction:(id)sender
    {
        datePicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 0,10, 50)];
        datePicker.datePickerMode = UIDatePickerModeDate;
        datePicker.hidden = NO;
        datePicker.date = [NSDate date];
        [datePicker addTarget:self action:@selector(labelTitle:) forControlEvents:UIControlEventValueChanged];
        [self.view addSubview:datePicker];
        rightBtn = [[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(save:)];
        self.navigationItem.rightBarButtonItem = rightBtn;       
    }
    
    -(void)labelTitle:(id)sender
    {
        NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
        dateFormat.dateStyle = NSDateFormatterMediumStyle;
        [dateFormat setDateFormat:@"MM/dd/yyyy"];
        NSString *str = [NSString stringWithFormat:@"%@",[dateFormat  stringFromDate:datePicker.date]];
        //assign text to label
        label.text=str; 
    }
    
    -(void)save:(id)sender
    {
        self.navigationItem.rightBarButtonItem = nil;
        [datePicker removeFromSuperview];       
    }
    

提交回复
热议问题