3

I want to add a pickerview instead of a keyboard for a specific textfield and fill the picker view with content directly in the .m file

What is the easiest way to accomplish that?

Can I do this directly in the interface builder or do i have to add much code on my own?

Brandon Minnick
  • 13,342
  • 15
  • 65
  • 123
Martin Huwa
  • 53
  • 1
  • 2
  • 6

5 Answers5

3

For any Xamarin.iOS developers, here's how to do it in C#

public partial class MyViewController : UIViewController
{
    public override void ViewDidLoad()
    {
        base.ViewDidLoad();

        ConfigurePicker();
    }

    void ConfigurePicker()
    {
        var pickerTextField = new UITextField();

        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
    {
        int[] _pickerSource = new []{ 1, 2, 3, 4, 5 };

        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;
    }
}
Brandon Minnick
  • 13,342
  • 15
  • 65
  • 123
  • 1
    Thank you for the direction on this. Only a couple of comments. First, pickerTextField.InputView = picker; (instead of emotionPicker). Second, I added an event handler for the doneButton to also handle setting the textfield of the selected item as the textfield wasn't updating. – ctc Oct 01 '18 at 18:10
  • 1
    Thanks @ctc! I fixed the `emotionPicker` typo. – Brandon Minnick Oct 01 '18 at 18:22
1

You can use custom button on uitextfield. and can fire action for that button is pressed. when pickerview dismiss then just add the selected text in textbox.

Mobile App Dev
  • 1,824
  • 3
  • 20
  • 45
1

Set tag to that textField and check condition in textFieldDidBeginEditing method write picker code.

Narayana Rao Routhu
  • 6,303
  • 27
  • 42
0
UIPickerView *picker = [[UIPickerView alloc] init];
picker.delegate = self;
picker.dataSource = self;
textField.inputView = picker;
Insider
  • 189
  • 2
  • 4
  • 9
0
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [self createPickerView]; 
}

-(void)createPickerView
{
    //create Picker Here
}

or Put Button over in place textfeild give inaction according to it.

Filip Radelic
  • 26,607
  • 8
  • 71
  • 97
Srinivas
  • 983
  • 9
  • 21