ToolBar完成按钮操作对UIDatePicker不起作用

时间:2015-06-08 10:58:21

标签: ios swift uibarbuttonitem uitoolbar uidatepicker

我以编程方式在工具栏中的UIDatePicker上设置完成并取消按钮。但是,我无法检测完成和取消按钮的单击操作。我找不到问题的原因。这是我的代码:

@IBAction func btnDueDate_Click(sender: UIButton)
{
    var toolBar = UIToolbar()
    toolBar.barStyle = UIBarStyle.Default
    toolBar.translucent = true
    toolBar.tintColor = UIColor(red: 92/255, green: 216/255, blue: 255/255, alpha: 1)
    toolBar.sizeToFit()

    var doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Plain, target: self, action: Selector("Done_Click"))
    var spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
    var cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Plain, target: self, action: Selector("Cancel_Click"))

    toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
    toolBar.userInteractionEnabled = true

    self.datePicker.addSubview(toolBar)
    self.datePicker.hidden = false
}

func Done_Click()
{
    var dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "dd-MM-yyyy"
    self.btnDueDate.setTitle(dateFormatter.stringFromDate(self.datePicker.date), forState: UIControlState.Normal)
}

func Cancel_Click()
{
    self.datePicker.hidden = true
}

你能帮我解决一下我做错了什么吗?

感谢您的回答

祝你好运

已解决:感谢您的帮助,我已通过编辑以下代码行解决了问题:

self.view.addSubview(toolBar)

当我将工具栏添加为self.view的子视图,而不是self.datePicker的子视图时,它可以正常工作。

3 个答案:

答案 0 :(得分:8)

您的代码无效的原因是UIToolbarUIDatePicker重叠,而选择器正在接收所有输入。

我建议你使用属性inputAccessoryView并将工具栏指定给它,但不幸的是,这是UIButtonUIDatePicker的只读属性

许多人正在使用的解决方法是使用UITextField并将inputView设置为UIDatePicker,将inputAccessoryView设置为UIToolbar 。我在下面做了一个小例子,你可以试试。

//
//  ViewController.swift
//  DatePickerWithToolbar
//
//  Created by Stefan Veis Pennerup on 10/06/15.
//  Copyright (c) 2015 Kumuluzz. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var tvDueDate: UITextField!
    var datePicker = UIDatePicker()

    override func viewDidLoad() {
        setupDatePicker()
    }

    func setupDatePicker() {
        // Sets up the "button"
        tvDueDate.text = "Pick a due date"
        tvDueDate.textAlignment = .Center

        // Removes the indicator of the UITextField
        tvDueDate.tintColor = UIColor.clearColor()

        // Specifies intput type
        datePicker.datePickerMode = .Date

        // Creates the toolbar
        let toolBar = UIToolbar()
        toolBar.barStyle = .Default
        toolBar.translucent = true
        toolBar.tintColor = UIColor(red: 92/255, green: 216/255, blue: 255/255, alpha: 1)
        toolBar.sizeToFit()

        // Adds the buttons
        var doneButton = UIBarButtonItem(title: "Done", style: .Plain, target: self, action: "doneClick")
        var spaceButton = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: nil, action: nil)
        var cancelButton = UIBarButtonItem(title: "Cancel", style: .Plain, target: self, action: "cancelClick")
        toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
        toolBar.userInteractionEnabled = true

        // Adds the toolbar to the view
        tvDueDate.inputView = datePicker
        tvDueDate.inputAccessoryView = toolBar
    }

    func doneClick() {
        var dateFormatter = NSDateFormatter()
        //dateFormatter.dateFormat = "dd-MM-yyyy"
        dateFormatter.dateStyle = .ShortStyle
        tvDueDate.text = dateFormatter.stringFromDate(datePicker.date)
        tvDueDate.resignFirstResponder()
    }

    func cancelClick() {
        tvDueDate.resignFirstResponder()
    }  
}

答案 1 :(得分:0)

dateFormatter.dateStyle = .ShortStyle
dateFormatter.dateFormat = "dd-MM-yyyy"

注意:在datesyle

下运行dateformat

答案 2 :(得分:0)

this答案中用完成按钮按下UIDatePicker时显示UIButton

var datePickerContainer = UIView()
let timePicker = UIDatePicker()

@IBAction func openTimePicker(sender: UIButton) {

    datePickerContainer.frame = CGRect(x: 0.0, y: (self.view.frame.height/2 + 30), width: self.view.frame.width, height: 250)
    datePickerContainer.backgroundColor = UIColor.white

    let doneButton = UIButton()
    doneButton.setTitle("Done", for: .normal)
    doneButton.setTitleColor(UIColor.blue, for: .normal)
    doneButton.addTarget(self, action: #selector(dismissPicker), for: UIControlEvents.touchUpInside)
    doneButton.frame = CGRect(x: 250.0, y: 5.0, width: 70.0, height: 40.0)
    datePickerContainer.addSubview(doneButton)

    timePicker.frame = CGRect(x: 0.0, y: 40.0, width: self.view.frame.width, height: 200.0)
    timePicker.datePickerMode = UIDatePickerMode.time
    timePicker.backgroundColor = UIColor.white
    timePicker.addTarget(self, action: #selector(CreateActivityViewController.startTimeDiveChanged), for: UIControlEvents.valueChanged)
    datePickerContainer.addSubview(timePicker)

    self.view.addSubview(datePickerContainer)
}

@objc func startTimeDiveChanged(sender: UIDatePicker) {
    let formatter = DateFormatter()
    formatter.timeStyle = .short
    expectedTimeBtn.setTitle(formatter.string(from: sender.date), for: .normal)
}

@objc func dismissPicker() {
    datePickerContainer.removeFromSuperview()

}