gpt4 book ai didi

iOS Swift 3-UIDatePicker

转载 作者:IT王子 更新时间:2023-10-29 05:12:40 25 4
gpt4 key购买 nike

我有一个显示日、月和年的 UIDatePicker。但我不知道如何获取这些参数。

那么使用 Swift 3 从 UIDatePicker 获取日、月和年的最佳方法是什么?

最佳答案

在 Swift 3 和 Swift 4 示例中的 UIDatePicker。

在 Swift 3 中你可以使用

  1. 在 swift 3 中创建单 View 应用程序的简单项目
  2. 拖动文本字段并连接到 viewController
  3. 日期选择器看起来像图像

enter image description here

  1. ViewController 代码如下。

      class ViewController: UIViewController {
    //Text Field Connection
    @IBOutlet weak var txtDatePicker: UITextField!
    //Uidate picker
    let datePicker = UIDatePicker()

    override func viewDidLoad() {
    super.viewDidLoad()
    //show date picker
    showDatePicker()
    }


    func showDatePicker(){
    //Formate Date
    datePicker.datePickerMode = .date

    //ToolBar
    let toolbar = UIToolbar();
    toolbar.sizeToFit()

    //done button & cancel button
    let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.bordered, target: self, action: "donedatePicker")
    let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
    let cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.bordered, target: self, action: "cancelDatePicker")
    toolbar.setItems([doneButton,spaceButton,cancelButton], animated: false)

    // add toolbar to textField
    txtDatePicker.inputAccessoryView = toolbar
    // add datepicker to textField
    txtDatePicker.inputView = datePicker

    }

    func donedatePicker(){
    //For date formate
    let formatter = DateFormatter()
    formatter.dateFormat = "dd/MM/yyyy"
    txtDatePicker.text = formatter.string(from: datePicker.date)
    //dismiss date picker dialog
    self.view.endEditing(true)
    }

    func cancelDatePicker(){
    //cancel button dismiss datepicker dialog
    self.view.endEditing(true)
    }
    }
  2. 完整的源代码项目已提供给 github。 github链接:https://github.com/enamul95/DatePicker1

    在 Swift 4 或 5 中你可以使用下面的代码

     class ViewController: UIViewController {

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

    override func viewDidLoad() {
    super.viewDidLoad()
    showDatePicker()
    }


    func showDatePicker(){
    //Formate Date
    datePicker.datePickerMode = .date

    //ToolBar
    let toolbar = UIToolbar();
    toolbar.sizeToFit()
    let doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(donedatePicker));
    let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
    let cancelButton = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(cancelDatePicker));

    toolbar.setItems([doneButton,spaceButton,cancelButton], animated: false)

    txtDatePicker.inputAccessoryView = toolbar
    txtDatePicker.inputView = datePicker

    }

    @objc func donedatePicker(){

    let formatter = DateFormatter()
    formatter.dateFormat = "dd/MM/yyyy"
    txtDatePicker.text = formatter.string(from: datePicker.date)
    self.view.endEditing(true)
    }

    @objc func cancelDatePicker(){
    self.view.endEditing(true)
    }
    }

关于iOS Swift 3-UIDatePicker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40484182/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com