Using the answer at this link TextInput Field with Pickerview instead of keyboard as a guide, I wrote the code below. I have three UITextFields that I need to configure a picker as input for. The second one is dependent on the selection of the first one, and the third one is dependent on the selection of the second one. I'm at a bit of a loss as to how I get the data into the class PickerViewModel
as each UITextField needs different data. The code with my modifications:
void ConfigurePicker(UITextField pickerTextField)
{
// var pickerTextField = new UITextField();
List<string> theData = new List<string>();
if ((pickerTextField.Placeholder.ToUpper() == "SELECT COMPANY" && dropTextField.Text == "") )
{
var okAlertController = UIAlertController.Create("ERROR", "Please select a company from the company drop down liat.", UIAlertControllerStyle.Alert);
okAlertController.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));
PresentViewController(okAlertController, true, null);
}else if (pickerTextField.Placeholder.ToUpper() == "SELECT SECTION" && dropTextField2.Text == "")
{
var okAlertController = UIAlertController.Create("ERROR", "Please select a department from the department drop down liat.", UIAlertControllerStyle.Alert);
okAlertController.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));
PresentViewController(okAlertController, true, null);
}
else
{
var picker = new UIPickerView
{
Model = new PickerViewModel(),
ShowSelectionIndicator = true
};
var screenWidth = UIScreen.MainScreen.Bounds.Width;
var pickerToolBar = new UIToolbar(new RectangleF(0, 0, (float)screenWidth, 44)) { BarStyle = UIBarStyle.Default, Translucent = true };
var flexibleSpaceButton = new UIBarButtonItem(UIBarButtonSystemItem.FlexibleSpace);
var doneButton = new UIBarButtonItem(UIBarButtonSystemItem.Done, (sender, e) => pickerTextField.ResignFirstResponder());
pickerToolBar.SetItems(new[] { flexibleSpaceButton, doneButton }, false);
pickerTextField.InputView = picker;
pickerTextField.InputAccessoryView = pickerToolBar;
}
}
class PickerViewModel : UIPickerViewModel
{
public List<string> _pickerSource = new List<string>();
public override nint GetRowsInComponent(UIPickerView pickerView, nint component) => _pickerSource.Count;
public override string GetTitle(UIPickerView pickerView, nint row, nint component) => _pickerSource[(int)row];
public override nint GetComponentCount(UIPickerView pickerView) => 1;
}