how to implement date picker on button click in ios?

巧了我就是萌 提交于 2019-12-09 14:52:03

问题


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 that is not able to implement date picker on button click and when user select the date,date picker hide,want to display selected date in a label.

I googled but could not find a good tutorial or library. I want to show only days and date in picker.


回答1:


-(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];       
}



回答2:


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];
}



回答3:


You can use various controls form this

https://www.cocoacontrols.com/search?utf8=%E2%9C%93&q=datepicker




回答4:


Add hidden textfield view and configure the date picker.

[self.xTextFeild setInputView:_datePicker];

Then When ever you want call below code.

[self.xTextFeild becomeFirstResponder];

`



来源:https://stackoverflow.com/questions/26796302/how-to-implement-date-picker-on-button-click-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!