UIDatePicker单击完成并显示文本

时间:2017-12-05 07:06:56

标签: ios swift uidatepicker

我想在UIPicker弹出窗口之后获取日期并直接点击完成按钮在“dobTxt.text”上显示日期而不滚动,并从UIDatePicker中选择日期。怎么做?

enter image description here

   func doneBtnForDate(){

    if (dobTxt.text == "Please select your DOB"){
         //let selectedDate = How to get picker date without scroll it?
         dobTxt.text = selectedDate
    }

    picker.isHidden = true
    toolBar2.isHidden = true
    datePicker.isHidden = true
}

4 个答案:

答案 0 :(得分:0)

更改您的doneBtnForDate,如下所示

func doneBtnForDate(){

    if (dobTxt.text == "Please select your DOB"){
         //let selectedDate = How to get picker date without scroll it?
         dobTxt.text = selectedDate
    }

   // IF you want to change date formatting then you need to change in "DateFormatter" below 
   let dateFormatter = DateFormatter()
    dateFormatter.dateStyle = .full
    dateFormatter.timeStyle = .full
    let strDate = dateFormatter.string(from: yourPicker.date)  
    dobTxt.text = strDate

    picker.isHidden = true
    toolBar2.isHidden = true
    datePicker.isHidden = true
} 

答案 1 :(得分:0)

这是客观的C代码。

     [datepicker addTarget:self  action:@selector(DateChange:)
            forControlEvents:UIControlEventValueChanged];

    //method for change date

    - (void)DateChange:(id)sender
{
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"dd-MM-yyyy"];
        label.text = [dateFormatter stringFromDate:datepicker.date];
        date.text=strDate;
}

答案 2 :(得分:0)

是否可以在dobTxt.inputView = picker方法

中设置viewDidLoad

对我来说很好。希望它会对你有所帮助。

答案 3 :(得分:0)

请试试这个:

func doneBtnForDate()
{
    if (dobTxt.text == "Please select your DOB")
    {
         let dateFormatter = DateFormatter()
         dateFormatter.dateFormat = "dd/MM/yyyy"
         dobTxt.text = dateFormatter.string(from: datePicker.date)
    }

    picker.isHidden = true
    toolBar2.isHidden = true
    datePicker.isHidden = true
}