uidatepicker始终滚动到当前时间

时间:2019-11-07 03:00:05

标签: ios swift uidatepicker

enter image description here enter image description here

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

我正在设置日期选择器。我正在尝试将datepicker和按钮连接到标签以显示正确的时间。 我已经将“尽快提取”按钮连接到“ @IBAction func buttonPressed”,如前面的代码所示,可以正常工作,即在按下时在“标签”中显示当前日期和时间(这里是2019 11 06 04:20:PM )(请参见上面的图片和下面的代码)。所以,这很好。

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

  1. 我的主要问题是,一旦按下“尽快提起”按钮,无论执行什么操作,日期选择器始终会滚动回到当前日期和时间。即使我将日期选择器滚动到其他日期,它也始终会滚动回到当前日期和时间。

  2. 另一个问题是,如果我滚动日期选择器而不单击“尽快提取”按钮,则“标签”不会显示日期选择器的任何日期和时间。我希望“标签”在滚动时显示日期选择器的日期和时间。

此外,当我按下“尽快提取”按钮或滚动日期选择器时,“标签”中的显示格式应该相同。

我在上面分别附有模拟器和情节提要的必要屏幕截图。

     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.
       }
     }

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

代码更新:

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.
    }
}