Search code examples
swiftuitextfielduipickerview

How to set pickerView and Keyboard for one textField


This is my first app. I have two text fields one for selecting Projects and second for selecting Tasks. If I select one project from the 1st text field and its showing related tasks in the 2nd text field. If task array is empty, then I have to show keyboard for entering some text for input to the 2nd text field. Please, someone, guide me how to achieve this task.

I have attached my project image


Solution

  • Let you have two UITextField as firstTextField and secondTextField. Then you can change the input view of UITextField as:

        if firstTextField.text == "" {
            secondTextField.inputView = nil
        } else {
            secondTextField.inputView = loadPicker()
        }
    

    Where,

    func loadPicker() -> UIPickerView {
        // Load picker and return from here
        return UIPickerView()
    }