如何将我的数据放入 PickerViewModel

How to get my data into the PickerViewModel

使用 link TextInput Field with Pickerview instead of keyboard 中的答案作为指导,我编写了以下代码。我有三个 UITextFields,我需要配置一个选择器作为输入。第二个取决于第一个的选择,第三个取决于第二个的选择。由于每个 UITextField 需要不同的数据,我对如何将数据输入 class PickerViewModel 有点不知所措。我修改后的代码:

    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;
    }

我在接孙子上班时得到了答案......这里是一个大“Duh”......实例化模型,将 属性 _pickerSource 设置为数据,设置pickerview 模型 MyModel ... 简单 ....

    void ConfigurePicker(UITextField pickerTextField, List<string> theData)
{

    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
    {
        PickerViewModel MyModel = new PickerViewModel();
        MyModel._pickerSource = theData;
        var picker = new UIPickerView
        {
            Model = MyModel,
            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;
}