在UIToolBar中实施“完成”按钮以关闭UIDatePicker和ToolBar

时间:2019-06-26 11:38:07

标签: swift button datepicker toolbar

我想通过按工具栏中的“完成”按钮来关闭uidatepicker和工具栏。

我使用的是标签而不是文本字段,因此常见的解决方案不起作用。

我尝试过:

@IBAction func ButtonPressed(_ sender: UIButton) {
    print("hallo")

    let picker = UIDatePicker()
    picker.backgroundColor = .white
    picker.datePickerMode = .date
    //picker.sizeToFit()


    var datumComponents = DateComponents()
    datumComponents.year = 2019
    datumComponents.month = 6
    datumComponents.day = 1
    let meinStartKalender = Calendar.current
    let StartDatum = meinStartKalender.date(from: datumComponents)

    picker.minimumDate = StartDatum
    picker.maximumDate = Date()
    let pickerSize1 = myView.bounds.width 
    picker.addTarget(self, action: #selector(dueDateChanged(sender:)), for: UIControl.Event.valueChanged)  
    picker.frame = CGRect(x:0.0, y: self.view.frame.height - 300 , width: pickerSize1, height: 200)
    self.view.addSubview(picker)



    let toolBar = UIToolbar()
    toolBar.barStyle = UIBarStyle.default
    toolBar.backgroundColor = .red
    toolBar.frame = CGRect(x:0.0, y: self.view.frame.height - 340 , width: pickerSize1, height: 40)
    self.view.addSubview(toolBar)

我已经找到的东西:

添加:

let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(self.donePressed(_:)))
toolBar.setItems([doneButton], animated: false)

,但错误消息显示:

  

“ ViewController”类型的值没有成员“ donePressed”

如何更改操作或如何实现有效的按钮?

1 个答案:

答案 0 :(得分:2)

将此添加到您的代码中

@objc
func donePressed(_ sender: UIButton) {

 }