如何在选择 UITextField 时显示 UIPickerView
这是我到目前为止所做的屏幕截图:
所以我想做的是当您选择“选择一个名字”文本字段我需要一个选择器来显示,输入@“Jack”。
Here is a screenshot of what I did till now:
So what I am trying to do is when you select "pick a name" Textfield I need a Picker to show up, with the input @"Jack".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
从 iOS 3.2 开始,
UITextField
支持inputView
属性来分配要用作键盘的自定义视图,这提供了一种显示UIPickerView
的方法:您可以使用
UITextField
的inputView
属性,可能与inputAccessoryView
属性结合使用。您将pickerView
分配给inputView
属性,并为inputAccessoryView
属性分配一个完成按钮来关闭选择器。就这样。这不会为您提供直接关闭视图的方法,因为您的 UIPickerView 没有返回按钮,这就是为什么我建议使用 inputAccessoryView 属性来显示带有完成按钮(该栏只是为了美观,您不妨使用
UIButton
对象):Since iOS 3.2,
UITextField
supports theinputView
property to assign a custom view to be used as a keyboard, which provides a way to display aUIPickerView
:You could use the
inputView
property of theUITextField
, probably combined with theinputAccessoryView
property. You assign yourpickerView
to theinputView
property, and, to dismiss the picker, a done button to theinputAccessoryView
property.Like that. This will not give you a direct way to dismiss the view since your
UIPickerView
has no return button, which is why I recommend to use theinputAccessoryView
property to display a toolbar with a done button (the bar is just for aesthetics, you might as well just use aUIButton
object):我使用这个并发现它比添加子视图和动画
UIPicker
干净得多I use this and find this a lot cleaner than adding a subview and animating the
UIPicker
它会为你工作..我已经编辑了它。为此你必须为文本字段设置委托。并在 NIb 文件中创建 UIPIckrView。
it will work for you .. i have edited it .and for that you have to set delegate for textfield. and create a UIPIckrView in NIb file.
好吧,您可以依靠
UITextFieldDelegate
来处理这种功能。内部
在
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
,您可以设置当前
UITextField
的文本以及初始化和显示UIPickerView< /代码>。
重要通知:
您可能还想遵守
UIPickerViewDelegate
。华泰
Well, you could rely on the
UITextFieldDelegate
to handle this kind of functionality.Inside the
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
is where you would set the text of your current
UITextField
as well as initializing and showing theUIPickerView
.Important notice:
You might also want to conform to the
UIPickerViewDelegate
.HTH
Swift:
像这样使用它:
Swift:
Use it like this:
您可以做的是,在
UITextField
上创建一个具有自定义类型的UIButton
。两者具有相同的尺寸。触摸按钮即可显示UIPickerView
。What you can do is, create a
UIButton
with custom type onUITextField
. Both having equal sizes. On the touch of button you can showUIPickerView
.http://tmblr.co/ZjkSZteCOUBS
我的博客中列出了代码和所有内容来准确执行此操作。但下面我列出了基本概念。
基本上,该解决方案涉及 github 上名为 ActionSheetPicker 的开源项目,并在
UITextFieldDelegate
上实现textFieldShouldBeginEditing
功能。您可以在那里关闭键盘并提供 UIPickerView。基本代码列于此处:http://tmblr.co/ZjkSZteCOUBS
I have the code and everything laid out in my blog to do this exactly. But below, I have the basic concept laid out.
Basically the solution involves an opensource project called ActionSheetPicker on github, and implementing the function
textFieldShouldBeginEditing
on theUITextFieldDelegate
. You can dismiss the keyboard there and provide a UIPickerView instead. The basic code is listed here:ViewController.h
ViewController.m
ViewController.h
ViewController.m