xcode 如果我单击 UITextField,如何使 UIPickerView 出现?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11096298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:38:15  来源:igfitidea点击:

How to make UIPickerView that appears if i click on the UITextField?

xcodeuipickerview

提问by lukso

How can i make a UIPickerView that appears when i select a TextField, and the input of this comes into TextField if i select a value of UIPickerView?

我如何制作一个在我选择 TextField 时出现的 UIPickerView,如果我选择 UIPickerView 的值,这个输入就会进入 TextField?

How to do this? Have you an example of this?

这该怎么做?你有这样的例子吗?

采纳答案by Melbourne

Please take a look at this example http://www.techotopia.com/index.php/An_iOS_4_iPhone_UIPickerView_Example

请看一下这个例子http://www.techotopia.com/index.php/An_iOS_4_iPhone_UIPickerView_Example

You can initially hide the picker by picker.hidden=YES; in your viewdidload method and for showing it when touch on the textfield you can write picker.hidden=NO; inside uitextfielddidstartediting method. Thanks.

您最初可以通过 picker.hidden=YES; 隐藏选择器。在您的 viewdidload 方法中并在触摸文本字段时显示它,您可以编写 picker.hidden=NO; 在 uitextfielddidstartediting 方法中。谢谢。

回答by jrturton

Create the UIPickerView and set it as the inputViewof your text field. It will then be animated on screen for you when you start editing the text field. The picker view delegate methods can be used to update the text field's value.

创建 UIPickerView 并将其设置为inputView您的文本字段。当您开始编辑文本字段时,它将在屏幕上为您设置动画。选择器视图委托方法可用于更新文本字段的值。

You will also want to create a toolbar with a done button and set this as the inputAccessoryView- this will allow you to dismiss the picker when you have selected your value. Again, this will appear automatically above the picker view when the text field begins editing.

您还需要创建一个带有完成按钮的工具栏并将其设置为inputAccessoryView- 这将允许您在选择值后关闭选择器。同样,当文本字段开始编辑时,这将自动出现在选择器视图上方。