首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将我的数据导入PickerViewModel

如何将我的数据导入PickerViewModel
EN

Stack Overflow用户
提问于 2020-07-03 04:12:28
回答 1查看 26关注 0票数 0

使用此链接TextInput Field with Pickerview instead of keyboard上的答案作为指南,我编写了以下代码。我有三个UITextFields,需要配置一个选取器作为输入。第二个依赖于第一个的选择,第三个依赖于第二个的选择。由于每个UITextField需要不同的数据,我对如何将数据放入class PickerViewModel感到有点困惑。代码和我的修改:

代码语言:javascript
复制
    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;
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-03 07:11:05

在我接孙子上班的时候我得到了答案。这里有一个很大的"Duh“...实例化模型,将属性_pickerSource设置为数据,将选择器视图模型设置为MyModel ...很简单……

代码语言:javascript
复制
    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;
} 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62704337

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档