使用sambag时间选择器时出现问题

时间:2018-12-27 12:39:21

标签: ios swift

我正在使用sambag时间选择器控制器,并且我在sambag Picker方法的按钮上设置了时间,但是问题是我在一个视图控制器中有多个按钮,所以我在设置时对每个非设置时间方法都执行了此操作按钮所有按钮的标题都改变了我想要的只是改变我按下的按钮的值

在这里我将向您展示我尝试过的内容,并在代码中向您展示

   import UIKit

   class ManageHoursViewController: UIViewController {

     var theme: SambagTheme = .light

    var result : String!
    @IBOutlet weak var btnMonStartTime: UIButton!
    @IBOutlet weak var btnMonEndTime: UIButton!
    @IBOutlet weak var btnMonOff: UIButton!
    @IBOutlet weak var btnMonOn: UIButton!


    override func viewDidLoad() {
        super.viewDidLoad()
        round()

        let hh2 = (Calendar.current.component(.hour, from: Date()))
        let mm2 = (Calendar.current.component(.minute, from: Date()))
        print(hh2)
        print(mm2)
        let dateTime = "\(hh2) : \(mm2)"
        btnMonStartTime.setTitle("\(dateTime)", for: .normal)
        btnMonEndTime.setTitle("\(dateTime)", for: .normal)
        // Do any additional setup after loading the view.
    }

    func round(){

        btnMonOff.isHidden = true
        btnMonStartTime.layer.cornerRadius = btnMonStartTime.frame.height / 2
        btnMonStartTime.layer.shadowColor = UIColor(red: 154/255, green: 154/255, blue: 154/255, alpha: 1).cgColor
        btnMonStartTime.layer.shadowOffset = CGSize(width: 0, height: 1)
        btnMonStartTime.layer.shadowOpacity = 1.0
        btnMonStartTime.layer.shadowRadius = 2.0
        btnMonStartTime.layer.masksToBounds = false

        btnMonEndTime.layer.cornerRadius = btnMonEndTime.frame.height / 2
        btnMonEndTime.layer.shadowColor = UIColor(red: 154/255, green: 154/255, blue: 154/255, alpha: 1).cgColor
        btnMonEndTime.layer.shadowOffset = CGSize(width: 0, height: 1)
        btnMonEndTime.layer.shadowOpacity = 1.0
        btnMonEndTime.layer.shadowRadius = 2.0
        btnMonEndTime.layer.masksToBounds = false
    }

    @IBAction func btnMonStartTimeTapped(_ sender: UIButton) {
        let vc = SambagTimePickerViewController()
        vc.theme = theme
        vc.delegate = self
        present(vc, animated: true, completion: nil)
    }
    @IBAction func btnMonEndTimeTapped(_ sender: UIButton) {
        let vc = SambagTimePickerViewController()
        vc.theme = theme
        vc.delegate = self
        present(vc, animated: true, completion: nil)
    }
}
extension ManageHoursViewController : SambagTimePickerViewControllerDelegate {

    func sambagTimePickerDidSet(_ viewController: SambagTimePickerViewController, result: SambagTimePickerResult) {
        self.result = "\(result)"
        print(self.result)
        btnMonStartTime.setTitle(self.result, for: .normal)
        btnMonEndTime.setTitle(self.result, for: .normal)
        viewController.dismiss(animated: true, completion: nil)
    }

    func sambagTimePickerDidCancel(_ viewController: SambagTimePickerViewController) {
        viewController.dismiss(animated: true, completion: nil)
    }
}

我希望像按星期一开始时间按钮一样然后选择时间表单选择器,然后现在仅更改星期一开始时间按钮值

1 个答案:

答案 0 :(得分:1)

您可以创建一个变量来跟踪最后点击的变量

 var lastBu: UIButton!


    @IBAction func btnMonStartTimeTapped(_ sender: UIButton) {
        let vc = SambagTimePickerViewController()
        vc.theme = theme
        vc.delegate = self
        lastBu = sender
        present(vc, animated: true, completion: nil) 
    }
    @IBAction func btnMonEndTimeTapped(_ sender: UIButton) {
        let vc = SambagTimePickerViewController()
        vc.theme = theme
        vc.delegate = self
        lastBu = sender
        present(vc, animated: true, completion: nil)
    }