如何在Swift 4中基于发件人更改在动作功能中使用的属性

时间:2018-06-28 16:32:41

标签: ios swift uislider

@IBOutlet weak var weekdaysHoursSliderOutlet: UISlider!
@IBOutlet weak var weekdaysHoursLabelOutlet: UILabel!
@IBOutlet weak var weekdaysHoursNumberLabel: UILabel!

@IBOutlet weak var weekdendsHoursSliderOutlet: UISlider!
@IBOutlet weak var weekendsHoursLabelOutlet: UILabel!
@IBOutlet weak var weekendsHoursNumberLabel: UILabel!


@IBAction func weekendsHoursSliderChange(_ sender: UISlider) {
    let currentValue: Int = Int(sender.value)
    weekendsHoursNumberLabel.text = ("\(currentValue)")

    if(currentValue == 1){
        weekendsHoursLabelOutlet.text = ("Hour Per Day")
    }
    else{
        weekendsHoursLabelOutlet.text = ("Hours Per Day")
    }
}

如果说weekdaysHourSliderOutlet调用了on change操作,我希望将其更改为工作日。我计划实现的不仅仅是周末和工作日,所以我不想一堆多余的代码。我当时在考虑使用某种字典,但无法正确连接它。预先谢谢你!

1 个答案:

答案 0 :(得分:0)

func viewDidLoad() {
     weekdaysHoursSliderOutlet.tag = 1
     weekdendsHoursSliderOutlet.tag = 2
}

@IBAction func sliderChanged(_ sender: UISlider) {
     if sender.tag == 1 {
          //handle weekdaysHoursSliderOutlet
     }
     else {
          //handle weekdendsHoursSliderOutlet
     }
}