Search code examples
swiftdatepickeruipickerview

UIPickerView is not selecting the date in UITextField


I am new in swift. On executing this app login screen goes to sign in.On selecting "Already have account sign up".You get to sign up view. In the sign up trying to develop the picker view.These are two issues arise while developing picker view

1) picker view should be hidden on loading view controller .but picker view is visible as view controller appear?

2) In date of birth pickview when a done button on date picker view is selected the app crash .but it should display the corresponding date in a text field?

func showDatePicker(){
        //Formate Date
        datePicker.datePickerMode = .date

        //ToolBar
        let toolbar = UIToolbar();
        toolbar.sizeToFit()


        let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.bordered, target: self, action: "donedatePicker")
        let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
        let cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.bordered, target: self, action: "cancelDatePicker")
        toolbar.setItems([doneButton,spaceButton,cancelButton], animated: false)

        dateTextField.inputAccessoryView = toolbar
        dateTextField.inputView = datePicker

    }
    func donedatePicker(){

        let formatter = DateFormatter()
        formatter.dateFormat = "dd/MM/yyyy"
        dateTextField.text = formatter.string(from: datePicker.date)
        self.view.endEditing(true)
    }

    func cancelDatePicker(){
        self.view.endEditing(true)
    }
  override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    pickerView.delegate=self
    pickerView.dataSource=self
    genderTextField.inputView=pickerView

    showDatePicker()

}

How to avoid this crash. You can download the project from this link https://drive.google.com/file/d/1a6Pmw2gVDwLP9grL8tG72NBx-tKwZozI/view?usp=sharing


Solution

  • you miss few things in view did load I had updated the code

    override func viewDidLoad() {
            super.viewDidLoad()
            pickerView.delegate=self
            pickerView.dataSource=self
            genderTextField.inputView = pickerView
            genderTextField.delegate = self
            dateTextField.delegate = self
    
        }
    

    Assign the text field delegate and implement the function textFieldShouldBeginEditing function for showing the Date Picker

    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
            if textField.tag == 1 { //Date Text Field
                showDatePicker()
            }
            return true
        }
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
            textField.resignFirstResponder()
            return true
        }
    

    I had assigned the tag value of Date Text Field from the storyboard and hide the picker view from the storyboard. It will solve your both points