点击文本字段时显示日期选择器
如何在点击文本框时显示日期时间选择器控件?
我有一个具有到达和出发文本字段的用户界面,当用户单击到达文本框时,它应该调出一个日期时间选择器控件而不是键盘,并且与出发文本框相同。
How can I display a datetimepicker control on tap of a textbox?
I have a user interface that has arrival and departure text fields, and when a user clicks on arrival textbox it should bring up a datetimepicker control up instead of a keyboard and the same with the departure textbox.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用文本字段的
inputView
和inputAccessoryView
属性来实现此目的。创建日期选择器并将其设置为两个文本字段的输入视图。还为Done
按钮创建另一个视图,并将其作为其附属视图。您将需要该按钮来关闭输入视图。Done
按钮必须连接到基本上执行此操作的函数 -另一种选择是子类
UITextField
并覆盖inputView
和inputAccessoryView
。当它们很多时,这是要走的路。示例
在 XIB 中,拉出一个
UIToolbar
和一个UIDatePicker
但不要将其附加到视图。正确连接插座。dateChanged:
响应日期选择器中的更改,并在单击工具栏中的Done
按钮时调用doneEditing:
。把它们也连接起来。这些方法的实施如下所列。随着更多文本字段依赖于此选择器,最后两种方法将会变得臃肿。
You can use
inputView
andinputAccessoryView
properties of the text field for this. Create the date picker and set it to the input views of the two text fields. Also create another view for theDone
button and it as their accessory view. You will need that button to dismiss the input view.The
Done
button must be wired up to function which basically does this –Another option would be to subclass
UITextField
and overrideinputView
andinputAccessoryView
. This is the way to go when there are loads of them.Example
In the XIB, Pull out a
UIToolbar
and aUIDatePicker
but don't attach it to the view. Connect the outlets appropriately.dateChanged:
responds to changes in the date picker anddoneEditing:
is called when theDone
button in the tool bar is clicked. Connect them too. The methods are implemented as listed below.The last two methods will bloat up as more text fields depend on this picker.
很简单,在
ViewController.h
中将UITextFieldDelegate
放在viewDidLoad
后面。Very simple, in
ViewController.h
put theUITextFieldDelegate
after inviewDidLoad
.在 viewDidLoad 中 ViewController.m
在 ViewController 中添加一个方法
txtFieldBranchYear 是 UITextField 的一个出口< /em>
In viewDidLoad of ViewController.m
Add one method in your ViewController
txtFieldBranchYear is an outlet of UITextField
我发现了一个非常有用的课程 - 博客文章在这里:http://www.pietrorea.com/2012/07/how-to-hide-the-cursor-in-a-uitextfield/ 但它基本上使用inputView/inputAccessory 视图也是如此。更棒的是,UILabel,至少对于我正在做的事情来说,正是我所需要的。
There's a really useful class I found - the blog post is here: http://www.pietrorea.com/2012/07/how-to-hide-the-cursor-in-a-uitextfield/ but it basically uses the inputView/inputAccessory view as well. The kicker is that the UILabel, at least for what I was doing, was exactly what I needed.
单击文本字段打开日期选择器 swift 3.0
添加 UITextFieldDelegate 和
在 ViewDidload() 上设置委托
tfDate.delegate = 自我
open date picker on click on textField swift 3.0
add UITextFieldDelegate and
set delegate on ViewDidload()
tfDate.delegate = self