开发者

TextInput Field with Pickerview instead of keyboard

I want to add a pickerv开发者_如何学Pythoniew 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?


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;
    }
}


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.


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


- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [self createPickerView]; 
}

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

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


UIPickerView *picker = [[UIPickerView alloc] init];
picker.delegate = self;
picker.dataSource = self;
textField.inputView = picker;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜