uidatepicker 总是滚动到当前时间

uidatepicker always rolls to current time

一旦按下按钮,日期选择器总是滚动到当前时间,而且日期选择器时间也不会显示。

我正在设置日期选择器。我正在尝试将日期选择器和按钮连接到标签以显示正确的时间。 我已将 "PickUp ASAP" 按钮连接到“@IBAction func buttonPressed”,如前面的代码所示,它工作正常,即按下时在 "Label" 中显示当前日期和时间(这里是 2019 11 06 04:20: PM)(见上面的图片和下面的代码)。所以,这里一切都好。

现在,我还尝试连接“@IBAction func datePickerChanged”以在"Label"滚动时显示当前日期和时间(见上图和下面的代码)。

  1. 我的主要问题是,一旦我按下 "PickUP ASAP" 按钮,无论我做什么,日期选择器总是滚动回到当前日期和时间。即使我将日期选择器滚动到不同的日期,它总是会滚动回到当前日期和时间。

  2. 另一个问题是,如果我滚动日期选择器而不单击 "Pickup ASAP" 按钮,"Label" 不会显示日期选择器的任何日期和时间。我希望 "Label" 在滚动时显示日期选择器的日期和时间。

此外,当我按下 "Pickup ASAP" 按钮或滚动日期选择器时,"Label" 中的显示格式应该相同。

我在上面分别附上了必要的模拟器截图和故事板。

     import UIKit

     @IBDesignable class testdateViewController: UIViewController {

     @IBOutlet weak var Label: UILabel!

     @IBOutlet weak var picker: UIDatePicker!

     @IBAction func buttonPressed(_ sender: UIButton) {

      picker.addTarget(self, action: #selector(buttonPressed), for: .valueChanged)
      let currentDateTime = Date()

      let formatter = DateFormatter()

      formatter.dateFormat = "yyyy MM dd hh:mm:  a"

      Label.text = " \( (formatter.string(from: currentDateTime)))"

      picker.date =  currentDateTime
    }

      override func viewDidLoad() {
       super.viewDidLoad()

     let datepicker = UIDatePicker()

     datepicker.addTarget(self, action: #selector(testdateViewController.datePickerChanged), for: UIControl.Event.valueChanged)
     }

     @IBAction func datePickerChanged(_ sender: Any) {

     let datePicker = UIDatePicker()
     let dateFormatter = DateFormatter()
     let strDate = dateFormatter.string(from: datePicker.date)
     Label.text = strDate
     }

     override func didReceiveMemoryWarning() {
     super.didReceiveMemoryWarning()
     // Dispose of any resources that can be recreated.
       }
     }
    1. My main problem is that once I press the "PickUP ASAP" Button, no matter what I do, the datepicker always scrolls back to the current date and time. Even if I scroll the datePicker to a different date, it always scrolls back to the current date and time.

    Answer:- Because you set the current date to date picker in "PickUP ASAP" Button action.

    2. Another problem is that "Label" does not show any date and time of the datepicker, if I scroll the datepicker without clicking the "Pickup ASAP" button. I want the "Label" to show the date and time of the datepicker when scrolled.

    Answer:- In `viewDidLoad()` please replace the line with `picker = UIDatePicker()` and remove 
             `let datePicker = UIDatePicker()` from `datePickerChanged` method. Also  Please specify the format for the `DateFormatter()` in `datePickerChanged` method.

 Look at the code below:-

override func viewDidLoad() {
    super.viewDidLoad()
    picker = UIDatePicker()
    picker.datePickerMode = .date
    picker.addTarget(self, action: #selector(testdateViewController.datePickerChanged), for: UIControl.Event.valueChanged)
}

func datePickerChanged(_ sender: Any) {
    setDateValue()
}

func setDateValue() {
    let dateFormat = DateFormatter()
    dateFormat.dateFormat = "yyyy/MM/dd"
    let str = dateFormat.string(from: picker.date)
    Label.text = str
}

@IBAction func buttonPressed(_ sender: UIButton) {
    setDateValue()
}

代码更新:

ViewDidLoad()

中删除此代码
let datepicker = UIDatePicker()
datepicker.addTarget(self, action: #selector(testdateViewController.datePickerChanged), for: UIControl.Event.valueChanged)

并在 ViewDidLoad()

中使用它
 picker.addTarget(self, action: #selector(testdateViewController.datePickerChanged), for: UIControl.Event.valueChanged)

将一些代码从 buttonPressed 方法移动到 datePickerChanged

datePickerChanged 看起来像

@IBAction func datePickerChanged(_ sender: Any) {
     updateData(date: picker.date) 
 } 

如果你想在标签中显示当前日期,那么你可以管理另一种方法来为标签设置值,就像这样

func updateData(date: Date) {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "yyyy MM dd hh:mm:  a"
    let strDate = dateFormatter.string(from: picker.date)
    Label.text = strDate
}

然后在 ViewDidLoad() 方法中添加此

 updateData(date: Date()) // This will show the current date value

最终 ViewDidLoad() 看起来像

override func viewDidLoad() {
   super.viewDidLoad() 
   picker.date =  Date() // You can set any date initially that you want. This will be a date object 
   picker.addTarget(self, action: #selector(testdateViewController.datePickerChanged), for: UIControl.Event.valueChanged)
   updateData(date: Date())
 }

并删除 buttonPressed 中的所有代码。因为在初始加载期间它会在标签上显示当前时间。

完整代码如下所示

import UIKit

class testdateViewController: UIViewController {

    @IBOutlet weak var Label: UILabel!
    @IBOutlet weak var picker: UIDatePicker!
    @IBAction func buttonPressed(_ sender: UIButton) {

    }

    override func viewDidLoad() {
        super.viewDidLoad()
        picker.date =  Date() // You can set any date initially that you want. This will be a date object
        picker.addTarget(self, action: #selector(testdateViewController.datePickerChanged), for: UIControl.Event.valueChanged)
        updateData(date: Date())

    }

    @IBAction func datePickerChanged(_ sender: Any) {
       updateData(date: picker.date)
    }

    func updateData(date: Date) {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy MM dd hh:mm:  a"
        let strDate = dateFormatter.string(from: picker.date)
        Label.text = strDate
    }

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