ios 按钮点击时的 UIDatePicker

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29165671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 05:17:11  来源:igfitidea点击:

UIDatePicker on button tap

iosxcodeswiftuidatepicker

提问by giuse

So far I've only found date-picker bound to UITextField, but I need a date-picker which opens on UIButtontap in the same way as it does with the UITextField.

到目前为止,我只发现日期选择器绑定到UITextField,但我需要一个日期选择器,UIButton它以与UITextField.

Is it possible? And if so, how? Thanks in advance.

是否可以?如果是这样,如何?提前致谢。

回答by iAnurag

@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)
}

回答by krishan kumar

because i dont have enough reputation to comment , i am posting same answer of @iAnurag in Swift 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)
}

回答by Syed Tariq

Here is another approach using .hidden feature of the controls. I use a button that changes the title when touched and hides/shows the date picker.

这是使用控件的 .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.
    }


}