Reputation: 589
not sure if this is a rookie question as I this is my first time using the UIPickerView.
My UIPickerView in my class has this array:
let cameraBodies = ["A","B","C","D","E","F","G","H","I","J",
"K","L","M","N","O","P","Q","R","S","T",
"U","V","W","X","Y","Z","Other"]
When the user clicks on my textField they have a choice of the above strings. If the user chooses "other", I want the UIPickerView to dismiss and change to a normal TextField so the user can input their own string. And then if the user deletes all the custom text in the textField it would return to the UIPickerView.
I thought about having 2 textfields on top of each other that I would show/hide depending on what the user chooses but I feel like this wouldnt be the correct way to approach this.
Any help on how to achieve this would be great.
Here is a section of my class that has everything to do with the UIPickerView:
class newCameraController : UIViewController{
var selectedBody: String?
var cameraBodyCustomTextField: UITextField = {
let textField = UITextField()
textField.placeholder = "Custom Camera Body"
return textField
}()
override func viewDidLoad() {
super.viewDidLoad()
createBodyPicker()
createToolBar()
}
func createBodyPicker() {
let bodyPicker = UIPickerView()
bodyPicker.delegate = self
cameraBodyTextField.inputView = bodyPicker
}
func createToolBar() {
let toolBar = UIToolbar()
toolBar.sizeToFit()
let doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(newCameraController.dismissKeyboard))
toolBar.setItems([doneButton], animated: false)
toolBar.isUserInteractionEnabled = true
cameraBodyTextField.inputAccessoryView = toolBar
}
override func dismissKeyboard() {
view.endEditing(true)
}
extension newCameraController: UIPickerViewDelegate, UIPickerViewDataSource {
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return cameraBodies.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return cameraBodies[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
selectedBody = cameraBodies[row]
cameraBodyTextField.text = selectedBody
}
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
var label: UILabel
if let view = view as? UILabel {
label = view
} else {
label = UILabel()
}
label.textColor = UIColor(red:0.63, green:0.63, blue:0.63, alpha:1.0)
label.textAlignment = .center
label.font = UIFont(name: "Avenir-Medium", size: 16.0)
label.text = cameraBodies[row]
return label
}
}
Upvotes: 0
Views: 685
Reputation: 3684
According to my opinion, the solution is: In your code, you are showing picker in place of a keyboard, it's correct. Now when a user selects "Other", simply show selected value "Other" in cameraBodyTextField. And show one extra text field just below cameraBodyTextField. Show/Hide can be managed by autolayout hight constraints with smoother animation.
Upvotes: 1