• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

c# - 如何将带有完成按钮的工具栏添加到 UIPickerView?

[复制链接]
菜鸟教程小白 发表于 2022-12-13 15:53:22 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我想在 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<ickerChangedEventArgs> 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; }
    }

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



Best Answer-推荐答案


只需将 UIToolbar 分配给 UITextFieldInputAccessoryView 属性即可。例如下面的代码片段:

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;

并像这样实现Action:

    [Export("DoneAction")]
    private void DoneAction()
    {
        Console.WriteLine("Your Action!");
    }

它是这样工作的:

enter image description here

关于c# - 如何将带有完成按钮的工具栏添加到 UIPickerView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47470238/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap