UIDatePicker和来自不同时间的两个按钮

时间:2018-01-22 00:38:10

标签: ios swift time uidatepicker

启动应用。

start app

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

choice of hour, and secondButton i  want different hour

我有两个按钮和一个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
    }

如何达到这个效果?

1 个答案:

答案 0 :(得分: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)
相关问题