点按钮上的UIDatePicker

时间:2015-03-20 11:39:15

标签: ios xcode swift uidatepicker

到目前为止,我发现日期选择器只绑定到UITextField,但我需要一个日期选择器,它在UIButton点击时打开,方式与UITextField相同。 1}}。

有可能吗?如果是这样,怎么样?提前谢谢。

4 个答案:

答案 0 :(得分:6)

@IBAction func BtnClicked(sender: AnyObject) {
    var picker : UIDatePicker = UIDatePicker()
    picker.datePickerMode = UIDatePickerMode.Date
    picker.addTarget(self, action: "dueDateChanged:", forControlEvents: UIControlEvents.ValueChanged)
    var pickerSize : CGSize = picker.sizeThatFits(CGSizeZero)
    picker.frame = CGRectMake(0.0, 250, pickerSize.width, 460)
    // you probably don't want to set background color as black
    // picker.backgroundColor = UIColor.blackColor()
    self.view.addSubview(picker)
}

func dueDateChanged(sender:UIDatePicker){
    var dateFormatter = NSDateFormatter()
    dateFormatter.dateStyle = NSDateFormatterStyle.LongStyle
    dateFormatter.timeStyle = NSDateFormatterStyle.NoStyle
    self.myLabel.text = dateFormatter.stringFromDate(dueDatePickerView.date)
}

答案 1 :(得分:4)

因为我没有足够的声誉来评论,我在Swift 4中发布@iAnurag的相同答案

 @IBAction func UpdateDOBClick(_ sender: UIButton) {
    let picker : UIDatePicker = UIDatePicker()
    picker.datePickerMode = UIDatePickerMode.date
    picker.addTarget(self, action: #selector(dueDateChanged(sender:)), for: UIControlEvents.valueChanged)
    let pickerSize : CGSize = picker.sizeThatFits(CGSize.zero)
    picker.frame = CGRect(x:0.0, y:250, width:pickerSize.width, height:460)
    // you probably don't want to set background color as black
    // picker.backgroundColor = UIColor.blackColor()
    self.view.addSubview(picker)
}
@objc func dueDateChanged(sender:UIDatePicker){
    let dateFormatter = DateFormatter()
    dateFormatter.dateStyle = .short
    dateFormatter.timeStyle = .none
dobButton.setTitle(dateFormatter.string(from: sender.date), for: .normal)
}

答案 2 :(得分:0)

这是使用控件的.hidden功能的另一种方法。我使用的按钮在触摸时更改标题并隐藏/显示日期选择器。

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var showDate: UILabel!
    @IBOutlet weak var myButtonx: UIButton!
    @IBAction func myButton(sender: UIButton) {
        if myButtonx.titleLabel?.text != "Done" {
            // save the date for your need
            showDate.text = "\(myDatePicker.date)"
            myDatePicker.hidden = false
            myButtonx.setTitle("Done",forState: UIControlState.Normal)
        } else {
            myDatePicker.hidden = true
            myButtonx.setTitle("Pick Date",forState: UIControlState.Normal)
        }
    }
    @IBOutlet weak var myDatePicker: UIDatePicker!
    override func viewDidLoad() {
        super.viewDidLoad()
        myDatePicker.hidden = true

        myButtonx.setTitle("Pick Date",forState: UIControlState.Normal)

        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

答案 3 :(得分:0)

private var datePicker: UIDatePicker?

@IBAction func dateFilterAction(_ sender: UIButton) {
    if self.view.subviews.contains(where: { $0 is UIDatePicker }) {
        self.datePicker?.removeFromSuperview()
        dateFilter.setTitle("Datum", for: .normal)
    } else {
        datePicker = nil
        setupDatePicker()
    }
}

private func setupDatePicker() {
    let picker = datePicker ?? UIDatePicker()
    picker.datePickerMode = .date
    if #available(iOS 13.4, *) {
        picker.preferredDatePickerStyle = .wheels
    }
    picker.addTarget(self, action: #selector(dueDateChanged(sender:)), for: .valueChanged)
    let size = self.view.frame.size
    picker.frame = CGRect(x: 0.0, y: size.height - 200, width: size.width, height: 200)
    picker.backgroundColor = UIColor.white
    self.datePicker = picker
    self.view.addSubview(self.datePicker!)
}

@objc func dueDateChanged(sender:UIDatePicker){
    let dateFormatter = DateFormatter()
    dateFormatter.dateStyle = .short
    dateFormatter.timeStyle = .none
    
    print("action")
    
    dateFilter.setTitle(dateFormatter.string(from: sender.date), for: .normal)
}