Swift:带有完成按钮的文本框上的弹出DatePicker

时间:2016-01-20 07:50:07

标签: ios swift datepicker

我正在创建一个包含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作为文本字段。没有工作。

在函数中添加注释表明datePickerValueChanged函数未被调用。

我做错了什么?

2 个答案:

答案 0 :(得分:5)

这里有一个带工具栏的日期选择器示例:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     android:id="@+id/drawer_layout"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:fitsSystemWindows="true">

     <!-- Your contents -->

     <android.support.design.widget.NavigationView
         android:id="@+id/navigation"
         android:layout_width="wrap_content"
         android:layout_height="match_parent"
         android:layout_gravity="start"
         app:menu="@menu/my_navigation_items" />
 </android.support.v4.widget.DrawerLayout>

答案 1 :(得分:2)

我相信datePickerValueChanged函数没有被调用,因为你还没有设置dateSampledPickerView的委托。

只需输入

即可
dateSampledPickerView.delegate = self

********* EDIT ****************

好的,仔细看后我相信你已经错误地设置了动作调用。替换此行

    dateSampledPickerView.addTarget(self, action: Selector("datePickerValueChanged"), forControlEvents: UIControlEvents.ValueChanged)

dateSampledPickerView.addTarget(self, action: "datePickerValueChanged", forControlEvents: UIControlEvents.ValueChanged)

删除Selector()