Xamarin iOS中的弹出选择器

时间:2017-09-26 13:31:14

标签: ios xamarin.ios

我需要像这张图片中那样实现一个选择器。我怎样才能做到这一点?我使用的是Xamarin.iOS,但我可以使用本机代码的答案并自行翻译。

Picker

3 个答案:

答案 0 :(得分:2)

使用ActionSheet

actionSheetButton.TouchUpInside += ((sender, e) => {

            // Create a new Alert Controller
            UIAlertController actionSheetAlert = UIAlertController.Create("Action Sheet", "Select an item from below", UIAlertControllerStyle.ActionSheet);

            // Add Actions
            actionSheetAlert.AddAction(UIAlertAction.Create("Item One",UIAlertActionStyle.Default, (action) => Console.WriteLine ("Item One pressed.")));

            actionSheetAlert.AddAction(UIAlertAction.Create("Item Two",UIAlertActionStyle.Default, (action) => Console.WriteLine ("Item Two pressed.")));

            actionSheetAlert.AddAction(UIAlertAction.Create("Item Three",UIAlertActionStyle.Default, (action) => Console.WriteLine ("Item Three pressed.")));

            actionSheetAlert.AddAction(UIAlertAction.Create("Cancel",UIAlertActionStyle.Cancel, (action) => Console.WriteLine ("Cancel button pressed.")));

            // Required for iPad - You must specify a source for the Action Sheet since it is
            // displayed as a popover
            UIPopoverPresentationController presentationPopover = actionSheetAlert.PopoverPresentationController;
            if (presentationPopover!=null) {
                presentationPopover.SourceView = this.View;
                presentationPopover.PermittedArrowDirections = UIPopoverArrowDirection.Up;
            }

            // Display the alert
            this.PresentViewController(actionSheetAlert,true,null);
        });

答案 1 :(得分:1)

您可以将PickerView分配给TextField的InputView属性,并将工具栏指定给InputAccessoryView,如下所示:

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;

textField.InputAccessoryView = toolBar;
textField.InputView = pickerView;

在您实施了挑选者视图的DataSourceDelegate之后,它可以像这样工作:

enter image description here

答案 2 :(得分:0)

感谢Kevin Li的回答,只是想给出一个简化的版本,以防有人需要。并非最初要求的那样,但工作确实完成。

        var pickerView = new UIPickerView();
        var sports = new string[] { "baseball", "basketball", "wiener dog" };
        var model = new PickerModel(sports);

        model.PickerChanged += (sender, e) =>
        {
            txtSport.Text = sports[e.SelectedValue];
        };

        pickerView.Model = model;
        pickerView.ShowSelectionIndicator = true;

        // setting picker view to your textfield
        txtSport.InputView = pickerView;