如何将带有完成按钮的ToolBar添加到UIPickerView?

时间:2017-11-24 09:35:09

标签: c# ios xamarin.ios uipickerview toolbar

我想在Xamarin.iOS项目中使用UIPickerView。我需要的UIPicker必须是这样的(默认隐藏,工具栏和完成按钮): Sample Picker for Xamarin.forms

这是Xamarin.forms的一个例子!

我已经看到所有关于堆栈溢出的问题,并且它们不在我的情况下,或者它们不是用于此目的的完整解释。

为了演示我已经尝试过创建完成工具栏,这是我的代码:

public class TestPickerViewController : UIViewController
    {
        PickerModel picker_model;
        UIPickerView picker;

        public TestPickerViewController()

        {
            Title = Texts.Home;
            View.BackgroundColor = UIColor.White;
            this.EdgesForExtendedLayout = UIRectEdge.None;

        }

        public override void DidReceiveMemoryWarning()
        {
            // Releases the view if it doesn't have a superview.
            base.DidReceiveMemoryWarning();

            // Release any cached data, images, etc that aren't in use.
        }

        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            List<Object> state_list = new List<Object>();
            state_list.Add("1");
            state_list.Add("2");
            state_list.Add("3");
            state_list.Add("4");
            picker_model = new PickerModel(state_list);

            picker = new UIPickerView();
            picker.Model = picker_model;
            picker.ShowSelectionIndicator = true;



            UIToolbar toolbar = new UIToolbar();
            toolbar.BarStyle = UIBarStyle.Black;
            toolbar.Translucent = true;
            toolbar.SizeToFit();

            UIBarButtonItem doneButton = new UIBarButtonItem("Done", UIBarButtonItemStyle.Done, (s, e) =>
            {
                foreach (UIView view in this.View.Subviews)
                {
                    if (view.IsFirstResponder)
                    {
                        UITextField textview = (UITextField)view;
                        textview.Text = picker_model.values[(int)picker.SelectedRowInComponent(0)].ToString();
                        textview.ResignFirstResponder();
                    }
                }

            });
            toolbar.SetItems(new UIBarButtonItem[] { doneButton }, true);


            View.AddSubviews(picker);

            //How to add toolbar, action for opening toolbar and hide by default the list 
        }

        public override void ViewDidLayoutSubviews()
        {
            base.ViewDidLayoutSubviews();
            View.SubviewsDoNotTranslateAutoresizingMaskIntoConstraints();

            View.AddConstraints(

                picker.AtTopOf(View, 90),
                picker.AtLeftOf(View, 50),
                picker.WithSameWidth(View).Minus(100)
            );
        }


        private void SetPicker(object sender, EventArgs e)
        {
            UITextField field = (UITextField)sender;
            picker.Select(picker_model.values.IndexOf(field.Text), 0, true);
        }
    }

    public class PickerModel : UIPickerViewModel
    {
        public IList<Object> values;

        public event EventHandler<PickerChangedEventArgs> PickerChanged;

        public PickerModel(IList<Object> values)
        {
            this.values = values;
        }

        public override nint GetComponentCount(UIPickerView picker)
        {
            return 1;
        }

        public override nint GetRowsInComponent(UIPickerView picker, nint component)
        {
            return values.Count;
        }

        public override string GetTitle(UIPickerView picker, nint row, nint component)
        {
            return values[(int)row].ToString();
        }

        public override nfloat GetRowHeight(UIPickerView picker, nint component)
        {
            return 40f;
        }

        public override void Selected(UIPickerView picker, nint row, nint component)
        {
            if (this.PickerChanged != null)
            {
                this.PickerChanged(this, new PickerChangedEventArgs { SelectedValue = values[(int)row] });
            }
        }
    }

    public class PickerChangedEventArgs : EventArgs
    {
        public object SelectedValue { get; set; }
    }

我知道我必须将工具栏添加到已经完成按钮的地方。我还需要隐藏默认选择器并在我们点击选择部分等时显示列表的操作......

1 个答案:

答案 0 :(得分:3)

只需将UIToolbar分配给InputAccessoryView的{​​{1}}媒体资源即可。这是一个代码段,例如:

UITextField

并按照以下方式实施Action:

UIToolbar toolBar = new UIToolbar(new CGRect(0, 0, 320, 44));
UIBarButtonItem flexibleSpaceLeft = new UIBarButtonItem(UIBarButtonSystemItem.FlexibleSpace,null,null);
UIBarButtonItem doneButton = new UIBarButtonItem("OK",UIBarButtonItemStyle.Done,this, new ObjCRuntime.Selector("DoneAction"));
UIBarButtonItem[] list = new UIBarButtonItem[] { flexibleSpaceLeft, doneButton };
toolBar.SetItems(list, false);

UIPickerView pickerView = new UIPickerView(new CGRect(0, 44, 320, 216));
pickerView.DataSource = new MyUIPickerViewDataSource();
pickerView.Delegate = new MyUIPickerViewDelegate();
pickerView.ShowSelectionIndicator = true;

//Assign the toolBar to InputAccessoryView 
textField.InputAccessoryView = toolBar;

textField.InputView = pickerView;

它的工作原理如下:

enter image description here