Uipickerview as Inputview of Uitextfield

UIPickerView as inputView of UITextField

Try this, it works fine, put it in viewdidload.

yourpicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 50, 100, 150)];
[yourpicker setDataSource: self];
[yourpicker setDelegate: self];
yourpicker.showsSelectionIndicator = YES;
self.yourtextfield.inputView = yourpicker;

do not [self.view addSubview: yourpicker]; this

UIPickerView as an inputView of multiple UITextFields

on your textfield delegate assign the inputview and tag for your textfield

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
textField.inputView = pcv
pcv.tag = textField.tag
return true;
}

and finally get the tag for pickerview for identify which textfield you tapped.

 func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
print("pickerView == \(pickerView.tag)")

}

Setting UITextField's inputView property to a UIPicker not working in Swift

The issue in your current code is in this method:

func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
itemPicker.hidden = false
return false // <--- this is not letting the textField become editable
}

I would suggest removing this method entirely.

Get UITextField from UIPickerView when using it as input view

If you have multiple textField that show pickerView then create one more instance of textField in your viewController like this and use this object inside textFieldDidBeginEditing and assign the reference of that textField to that tempTextField.

var selectedTextField: UITextField = UITextField()

func textFieldDidBeginEditing(textField: UITextField!) {
self.selectedTextField = textField
}

Now in didSelectRow method of PickerView use this textField o know the current editing field.

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if self.selectedTextField == countryField {
//Set text for countryField
}
else self.selectedTextField == stateField {
//Set text for stateField
}
}

Note: For example I have used countryField and stateField you can use textField for that you want to set text.

How do I reset a UIPickerView when it is associated with a TextField's inputView?

Once solution would be to implement the func textFieldDidBeginEditing(_ textField: UITextField) delegate method. And then see if the text field's inputView is a picker view. If so, reset the picker view as needed.

func textFieldDidBeginEditing(_ textField: UITextField) {
if let picker = textField.inputView as? UIPickerView {
picker.selectRow(0, inComponent: 0, animated: false)
}
}

If you have multiple text fields that use a picker view and you only want to reset the picker for some of the text fields, then add a check to see if textField is one of the ones you need to handle.

Is UITextField+UIPickerView supported under Mac Catalyst?

It looks like a bug in Mac Catalyst. Try using the UIPicker embedded in an UIActionSheet. ActionSheetPicker-3.0 is a ready-made solution. Show the ActionSheetPicker in textFieldDidBeginEditing of your UITextField.

Keyboard is showing on first tap for a UITextField.inputView = UIPickerView

Make sure that you are setting your UITextFieldDelegate, and then I would also move all of this:

let motivoPicker: UIPickerView = UIPickerView()

motivos = ["Pedido de demissão","Dispensa sem justa causa","Dispensa com justa causa", "Término do contrato de experiência"]

motivoPicker.delegate = self
motivoPicker.dataSource = self
motivoTextField.inputView = motivoPicker

to viewDidLoad



Related Topics



Leave a reply



Submit