Swift:在带有完成按钮的文本框中弹出 DatePicker
Posted
技术标签:
【中文标题】Swift:在带有完成按钮的文本框中弹出 DatePicker【英文标题】:Swift: Popup DatePicker on textbox with done button 【发布时间】:2016-04-25 23:05:11 【问题描述】:我正在创建一个应用程序,上面有一个 UITextField,当点击/单击它时,将显示一个从底部弹出的日期选择器。所述弹出面板有一个完成按钮,该按钮将在单击/点击时将选择的任何日期(默认为今天)应用到文本字段,最好是 YYYY-MM-DD 格式。
我一直在网上搜索,到目前为止,我已经看到了带有 IBAction 文本字段的示例,以及关于完成按钮如何工作的各种方式(并且大多数没有完成按钮),但我想知道如果我可以在下面的下拉菜单中回收或基于我的代码:
let wellheadPressTest : [String] = ["PSIG", "MPAG"]
var wellboreStatusPickerView = UIPickerView()
override func viewDidLoad()
let wellboreStatusToolBar = UIToolbar()
let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
let doneButtonWellbore = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Plain, target: self, action: "donePickerWellbore")
wellboreStatusToolBar.barStyle = UIBarStyle.Default
wellboreStatusToolBar.translucent = true
wellboreStatusToolBar.tintColor = UIColor(red: 76/255, green: 217/255, blue: 100/255, alpha: 1)
wellboreStatusToolBar.sizeToFit()
wellboreStatusToolBar.setItems([spaceButton,spaceButton,doneButtonWellbore], animated: false)
wellboreStatusToolBar.userInteractionEnabled = true
wellboreStatusPickerView.delegate = self
self.samplingWaterFormOneView.textfieldWellbore.inputView = wellboreStatusPickerView
self.samplingWaterFormOneView.textfieldWellbore.inputAccessoryView = wellboreStatusToolBar
// Various Required PickerView functions
func donePickerWellbore()
let row = wellboreStatusPickerView.selectedRowInComponent(0);
pickerView(wellboreStatusPickerView, didSelectRow: row, inComponent:0)
self.samplingWaterFormOneView.textfieldWellbore.resignFirstResponder()
这是我尝试过的:
var dateSampledPickerView = UIDatePicker()
override func viewDidLoad()
let dateSampledPickerToolBar = UIToolbar()
let doneButtonDate = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Plain, target: self, action: "donePickerDate")
dateSampledPickerToolBar.barStyle = UIBarStyle.Default
dateSampledPickerToolBar.translucent = true
dateSampledPickerToolBar.tintColor = UIColor(red: 76/255, green: 217/255, blue: 100/255, alpha: 1)
dateSampledPickerToolBar.sizeToFit()
dateSampledPickerToolBar.setItems([spaceButton,spaceButton,doneButtonDate], animated: false)
dateSampledPickerToolBar.userInteractionEnabled = true
dateSampledPickerView.datePickerMode = UIDatePickerMode.Date
dateSampledPickerView.addTarget(self, action: Selector("datePickerValueChanged"), forControlEvents: UIControlEvents.ValueChanged)
self.samplingWaterFormOneView.textfieldDateSampled.inputView = dateSampledPickerView
self.samplingWaterFormOneView.textfieldDateSampled.inputAccessoryView = dateSampledPickerToolBar
self.samplingWaterFormOneView.textfieldDateSampled.delegate = self
func datePickerValueChanged(sender:UIDatePicker)
let dateFormatter = NSDateFormatter()
dateFormatter.dateStyle = "YYYY-MM-DD"
dateFormatter.timeStyle = NSDateFormatterStyle.NoStyle
self.samplingWaterFormOneView.textfieldDateSampled.text = dateFormatter.stringFromDate(sender.date)
func donePickerDate()
self.samplingWaterFormOneView.textfieldDateSampled.resignFirstResponder()
当我在文本字段顶部时,上面的代码确实显示了一个带有日期选择器的弹出菜单,带有一个完成按钮,但文本字段没有获取日期。
编辑:还尝试修改datePickerValueChanged
使其没有发件人,并且还使用dateSampledPickerView
作为文本字段。没用。
在函数中加入 cmets 表明 datePickerValueChanged
函数没有被调用。
我做错了什么?
【问题讨论】:
【参考方案1】:这里有一个带有工具栏的日期选择器示例:
var date: NSDate?
var timePicker: UIDatePicker?
@IBOutlet weak var dateLabel: UILabel!
func initDatePicker()
if self.timePicker == nil
self.timePicker = UIDatePicker()
self.timePicker!.datePickerMode = UIDatePickerMode.Date
self.timePicker!.addTarget(self, action: "updateDate", forControlEvents: UIControlEvents.ValueChanged)
self.timePicker!.minimumDate = NSDate()
self.timePicker!.backgroundColor = UIColor().backgroundColor()
self.dateLabel.inputView = self.timePicker
self.dateLabel.inputAccessoryView = self.createPickerToolBar()
func updateDate()
let picker: UIDatePicker = self.dateLabel.inputView as! UIDatePicker
self.dateLabel.text = picker.date.stringFromDate(kDateFormatSlash)
self.date = picker.date
self.delegate?.didIntroduceText(self.actionTextView.text.characters.count != 0 && self.searchableTableView?.getText().characters.count != 0 && self.dateLabel.text!.characters.count != 0)
func createPickerToolBar() -> UIToolbar
let toolbar = UIToolbar()
let doneButton = UIBarButtonItem(title: "DONE", style: UIBarButtonItemStyle.Done, target: self, action: "doneAction")
doneButton.setTitleTextAttributes([NSFontAttributeName: UIFont(name: kOpenSansRegular, size: kHeaderFontSize)!, NSForegroundColorAttributeName: UIColor.whiteColor()], forState: UIControlState.Normal)
doneButton.accessibilityLabel = "DoneToolbar"
let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
toolbar.translucent = false
toolbar.sizeToFit()
toolbar.setItems([spaceButton, doneButton], animated: false)
toolbar.userInteractionEnabled = true
return toolbar
func doneAction()
self.updateDate()
self.dateLabel.resignFirstResponder()
self.timePicker?.removeFromSuperview()
【讨论】:
我在这里遗漏了一些东西。我有两个文本字段,每个都使用一个日期选择器,我必须复制这些函数中的每一个来分隔它们。这似乎是错误的,即使它有效。如何告诉 doneAction 在一个函数中更新正确的字段而不是重复? 您可以在日期选择器和工具栏上添加标签【参考方案2】:我相信 datePickerValueChanged 函数没有被调用,因为您没有设置 dateSampledPickerView 的委托。
所以直接输入
dateSampledPickerView.delegate = self
*********编辑****************
好的,仔细观察后,我相信您错误地设置了动作调用。替换这一行
dateSampledPickerView.addTarget(self, action: Selector("datePickerValueChanged"), forControlEvents: UIControlEvents.ValueChanged)
与
dateSampledPickerView.addTarget(self, action: "datePickerValueChanged", forControlEvents: UIControlEvents.ValueChanged)
移除 Selector()
【讨论】:
嗯……我也是这么想的,但显然Value of UIDatePicker has no member delegate
。
谢谢,这让我有所收获。现在我通过 print 知道该函数正在被调用。但是,它不会更新文本字段。我尝试打印出dateFormatter.stringFromDate(dateSampledPickerView.date)
,但我什么也没得到。
有趣。您是否尝试打印出 dateSampledPickerView.date 或 dateFormatter.stringFromDate(sender.date)?还要澄清“完成”按钮是否有效?比如,选择器视图会消失吗?
我尝试在我的初始代码中打印出 dateFormatter.stringFromDate(sender.date),并为我的麻烦得到了一个 unrecognized selector sent to instance
- 它发生在更改 DatePicker 的值时。我想也许它与发件人有关,所以我按照上面的代码删除了它。 “完成”按钮有效,它使弹出窗口消失,但不会向文本字段写入任何内容。
重新添加了 Selector("datePickerValueChanged:") 和 (sender: UIDatePicker) 修复了它(冒号显然很重要。但是,我又回到了第一方。它正在打印一个空行。以上是关于Swift:在带有完成按钮的文本框中弹出 DatePicker的主要内容,如果未能解决你的问题,请参考以下文章