ios 用于文本字段输入的 Swift UIDatePicker - 如何使 UIDatePicker 消失?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26985603/
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
Swift UIDatePicker for text field input - how to make UIDatePicker disappear?
提问by MGann
This is my first ever attempt at Swift or an iPhone app, so needless to say I'm not sure how to do many things at this point.
这是我第一次尝试使用 Swift 或 iPhone 应用程序,所以不用说我现在不确定如何做很多事情。
Right now I have a view controller with a text field and a button. I've managed to find example code to almost accomplish what I need. I'd like to do the following:
现在我有一个带有文本字段和按钮的视图控制器。我设法找到了几乎可以完成我需要的示例代码。我想做以下事情:
- When I click into the text field have a
UIDatePicker
come on screen. - Set the
UIDate
picker to display time only - not a date. - Put the selected
UIDatePicker
time in the textfield. - Have the
UIDatePicker
go away when I click on the button.
- 当我点击文本字段时
UIDatePicker
,屏幕上会出现一个。 - 将
UIDate
选择器设置为仅显示时间 - 而不是日期。 - 将选定的
UIDatePicker
时间放在文本字段中。 - 有
UIDatePicker
走开当我点击的按钮。
1 through 3 is working. But I can't figure out how to have the UIDatePicker
go away and still come back on the screen if I click into the text field again.
1 到 3 正在工作。但是,UIDatePicker
如果我再次单击文本字段,我不知道如何让它消失并仍然回到屏幕上。
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var dateField: UITextField!
@IBAction func dateField(sender: UITextField) {
var datePickerView : UIDatePicker = UIDatePicker()
datePickerView.datePickerMode = UIDatePickerMode.Time
sender.inputView = datePickerView
datePickerView.addTarget(self, action: Selector("handleDatePicker:"), forControlEvents: UIControlEvents.ValueChanged)
}
@IBAction func DoneButton(sender: UIButton) {
//How to make datepicker disappear???
}
func handleDatePicker(sender: UIDatePicker) {
var timeFormatter = NSDateFormatter()
timeFormatter.dateStyle = .NoStyle
timeFormatter.timeStyle = .ShortStyle
dateField.text = timeFormatter.stringFromDate(sender.date)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
}
采纳答案by Nate Cook
You call resignFirstResponder
on the text field:
您resignFirstResponder
在文本字段上调用:
@IBAction func DoneButton(sender: UIButton) {
dateField.resignFirstResponder()
}
Also, it's not a great idea to have a method and a property with the same name - not sure about precedence there, but chances are it could be confusing at some point.
此外,拥有同名的方法和属性也不是一个好主意 - 不确定那里的优先级,但在某些时候可能会令人困惑。
回答by ZaMy
You could try something as gestureRecognizer
to make the UIDatePicker disappear:
您可以尝试gestureRecognizer
使 UIDatePicker 消失的方法:
@objc func viewTapped(gestureRecognizer: UITapGestureRecognizer) {
view.endEditing(true)
}
This is what I used in my app so when the user taps on anywhere on the screen the UIDatePicker goes away!
这就是我在我的应用程序中使用的,所以当用户点击屏幕上的任何地方时 UIDatePicker 消失了!
回答by Edrick Pascual
You can also just create an outlet for the datePicker. @IBOutlet weak var datePicker: UIPickerView!
您也可以为 datePicker 创建一个插座。@IBOutlet 弱变量 datePicker:UIPickerView!
then in your function
然后在你的函数中
@IBAction func DoneButton(sender: UIButton) {
datePicker.hidden = true
}