不同时间的 UIDatePicker 和两个按钮

UIDatePicker and Two Buttons from different time

启动应用程序。

小时和秒按钮的选择。我想要不同的时间。

我有两个按钮和一个 DatePicker。 set datePickerMode = .time。 我需要在不同的按钮上设置两个不同的时间。 不幸的是,我找不到解决方案。

这是我的代码:

class ViewController: UIViewController  {
    override func viewDidLoad() {
        super.viewDidLoad()

        view.addSubview(firstButton)
        firstButton.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 100).isActive = true
        firstButton.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 30).isActive = true
        firstButton.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -30).isActive = true
        firstButton.heightAnchor.constraint(equalToConstant: 100).isActive = true

        view.addSubview(secondButton)
        secondButton.topAnchor.constraint(equalTo: firstButton.bottomAnchor, constant: 10).isActive = true
        secondButton.leftAnchor.constraint(equalTo: firstButton.leftAnchor).isActive = true
        secondButton.rightAnchor.constraint(equalTo: firstButton.rightAnchor).isActive = true

        view.addSubview(myDatePicker)
        myDatePicker.topAnchor.constraint(equalTo: secondButton.bottomAnchor, constant: 10).isActive = true
        myDatePicker.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 10).isActive = true
        myDatePicker.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -10).isActive = true
    }

    // MyFirstButton
    let firstButton: UIButton = {
        let button = UIButton(type: .system)
        button.setTitle("First", for: .normal)
        button.translatesAutoresizingMaskIntoConstraints = false
        button.tintColor = .white
        button.tag = 0
        button.addTarget(self, action: #selector(handleButton), for: .touchUpInside)
        return button
    }()

    // MySecondButton
    let secondButton: UIButton = {
        let button = UIButton(type: .system)
        button.setTitle("Second", for: .normal)
        button.translatesAutoresizingMaskIntoConstraints = false
        button.tintColor = .white
        button.tag = 1
        button.addTarget(self, action: #selector(handleButton), for: .touchUpInside)
        return button
    }()

    // Target First and Second Button
    @objc func handleButton() {
        view.layoutIfNeeded()
        UIView.animate(withDuration: 0.25, animations: {
            self.view.layoutIfNeeded()
        }) { (completed) in
            UIView.animate(withDuration: 0.25, animations: {
                self.myDatePicker.alpha = 1
                self.view.layoutIfNeeded()
            })
        }
    }

    // DataPicker
    let myDatePicker: UIDatePicker = {
        let pv = UIDatePicker()
        pv.timeZone = .current
        pv.datePickerMode = .time
        pv.backgroundColor = .white
        pv.translatesAutoresizingMaskIntoConstraints = false
        pv.addTarget(self, action: #selector(handleMyDatePicker), for: .valueChanged)
        return pv
    }()

    // Target
    @objc func handleMyDatePicker(sender: UIDatePicker) {
        let formatter = DateFormatter()
        formatter.timeStyle = .short
        let selectedTime = formatter.string(from: sender.date)

        firstButton.setTitle(selectedTime, for: .normal)

        // here is the problem
        secondButton.setTitle(selectedTime, for: .normal)

        myDatePicker.alpha = 0
    }

如何实现这种效果?

将您的 myDatePicker 标签设置为与您触摸的按钮相同

@objc func handleButton(button: UIButton) {
    view.layoutIfNeeded()
    UIView.animate(withDuration: 0.25, animations: {
        self.view.layoutIfNeeded()
    }) { (completed) in
        UIView.animate(withDuration: 0.25, animations: {
            self.myDatePicker.alpha = 1

            self.myDatePicker.tag = button.tag
            self.view.layoutIfNeeded()
        })
    }
}

@objc func handleMyDatePicker(sender: UIDatePicker) {
    let formatter = DateFormatter()
    formatter.timeStyle = .short
    let selectedTime = formatter.string(from: sender.date)
    let button = self.view.viewWithTag(sender.tag) as? UIButton
    button?.setTitle(selectedTime, for: .normal)
    myDatePicker.alpha = 0
}

您的 addTarget 代码略有改动

button.addTarget(self, action: #selector(handleButton(button:)), for: .touchUpInside)