使用 Pickerview 而不是键盘的 TextInput 字段

发布于 2024-12-02 17:42:06 字数 114 浏览 2 评论 0原文

我想为特定文本字段添加选取器视图而不是键盘,并直接在 .m 文件中使用内容填充选取器视图

实现此目的最简单的方法是什么?

我可以直接在界面生成器中执行此操作还是必须自己添加大量代码?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

眼趣 2024-12-09 17:42:06

对于任何 Xamarin.iOS 开发人员,以下是如何使用 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;
    }
}

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;
    }
}
邮友 2024-12-09 17:42:06

您可以在 uitextfield 上使用自定义按钮。并可以在按下该按钮时触发操作。
当 pickerview 关闭时,只需在文本框中添加选定的文本。

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.

<逆流佳人身旁 2024-12-09 17:42:06

将标签设置为该文本字段并检查 textFieldDidBeginEditing 方法中的条件编写选择器代码。

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

圈圈圆圆圈圈 2024-12-09 17:42:06
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [self createPickerView]; 
}

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

或将按钮放在适当的位置,textfield 根据它给出不动作。

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

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

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

假扮的天使 2024-12-09 17:42:06
UIPickerView *picker = [[UIPickerView alloc] init];
picker.delegate = self;
picker.dataSource = self;
textField.inputView = picker;
UIPickerView *picker = [[UIPickerView alloc] init];
picker.delegate = self;
picker.dataSource = self;
textField.inputView = picker;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文