合并按钮操作以减少重复

时间:2016-05-29 20:34:17

标签: ios swift uibutton code-duplication

我有现在的代码,允许选择重复事件的星期几。目前它们是独立的,重复性很高。

如何重构按钮以减少重复?

// Array of days repeating:
    var weekDayRepeat = [false,false,false,false,false,false,false]

    var savedEventId : String = ""

    @IBOutlet weak var datePickerStart: UIDatePicker!

    @IBOutlet weak var datePickerEnd: UIDatePicker!

    @IBOutlet weak var repeatSwitch: UISwitch!

    @IBOutlet weak var monSelect: UIButton!

    @IBOutlet weak var tuesSelect: UIButton!

    @IBOutlet weak var wedsSelect: UIButton!

    @IBOutlet weak var thursSelect: UIButton!

    @IBOutlet weak var friSelect: UIButton!

    @IBOutlet weak var satSelect: UIButton!

    @IBOutlet weak var sunSelect: UIButton!

    @IBOutlet weak var repeatingLabel: UILabel!

    @IBAction func monSelect(sender: AnyObject) {
        if(weekDayRepeat[0]) {
            monSelect.setTitleColor(UIColor.lightGrayColor(), forState: UIControlState.Normal)

            weekDayRepeat[0] = false

        } else {
            monSelect.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)

            weekDayRepeat[0] = true

        }

    }

    @IBAction func tuesSelect(sender: AnyObject) {
        if(weekDayRepeat[1]) {
            tuesSelect.setTitleColor(UIColor.lightGrayColor(), forState: UIControlState.Normal)

            weekDayRepeat[1] = false

        } else {
            tuesSelect.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)

            weekDayRepeat[1] = true

        }
    }

    @IBAction func wedsSelect(sender: AnyObject) {
        if(weekDayRepeat[2]) {
            wedsSelect.setTitleColor(UIColor.lightGrayColor(), forState: UIControlState.Normal)

            weekDayRepeat[2] = false

        } else {
            wedsSelect.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)

            weekDayRepeat[2] = true

        }
    }

    @IBAction func thursSelect(sender: AnyObject) {
        if(weekDayRepeat[3]) {
            thursSelect.setTitleColor(UIColor.lightGrayColor(), forState: UIControlState.Normal)

            weekDayRepeat[3] = false

        } else {
            thursSelect.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)

            weekDayRepeat[3] = true

        }
    }

    @IBAction func friSelect(sender: AnyObject) {
        if(weekDayRepeat[4]) {
            friSelect.setTitleColor(UIColor.lightGrayColor(), forState: UIControlState.Normal)

            weekDayRepeat[4] = false

        } else {
            friSelect.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)

            weekDayRepeat[4] = true

        }

    }

    @IBAction func satSelect(sender: AnyObject) {
        if(weekDayRepeat[5]) {
            satSelect.setTitleColor(UIColor.lightGrayColor(), forState: UIControlState.Normal)

            weekDayRepeat[5] = false

        } else {
            satSelect.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)

            weekDayRepeat[5] = true

        }
    }

    @IBAction func sunSelect(sender: AnyObject) {
        if(weekDayRepeat[6]) {
            sunSelect.setTitleColor(UIColor.lightGrayColor(), forState: UIControlState.Normal)

            weekDayRepeat[6] = false

        } else {
            sunSelect.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)

            weekDayRepeat[6] = true

        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // Set initial conditions of the page...:
        repeatSwitch.on = true

        datePickerStart.datePickerMode = UIDatePickerMode.Time
        datePickerEnd.datePickerMode = UIDatePickerMode.Time

        monSelect.setTitleColor(UIColor.lightGrayColor(), forState: UIControlState.Normal)
        tuesSelect.setTitleColor(UIColor.lightGrayColor(), forState: UIControlState.Normal)
        wedsSelect.setTitleColor(UIColor.lightGrayColor(), forState: UIControlState.Normal)
        thursSelect.setTitleColor(UIColor.lightGrayColor(), forState: UIControlState.Normal)
        friSelect.setTitleColor(UIColor.lightGrayColor(), forState: UIControlState.Normal)
        satSelect.setTitleColor(UIColor.lightGrayColor(), forState: UIControlState.Normal)
        sunSelect.setTitleColor(UIColor.lightGrayColor(), forState: UIControlState.Normal)


        // Allow dynamically changing the mode given if repeating or not.
        // If switch to repeating, then show selecting only the time and list of days to select/multi-select
        repeatSwitch.addTarget(self, action: #selector(ViewController.switchChanged(_:)), forControlEvents: UIControlEvents.ValueChanged)

    }

2 个答案:

答案 0 :(得分:1)

尝试以下代码:

@IBOutlet var weekDayBtns: [UIButton]!

@IBAction func weekDayBtnAction(sender: UIButton) {

    weekDayRepeat[sender.tag] = !weekDayRepeat[sender.tag]
    sender.setTitleColor(weekDayRepeat[sender.tag] ? UIColor.blackColor():UIColor.lightGrayColor()  , forState: UIControlState.Normal)

}

override func viewDidLoad() {
    super.viewDidLoad()
    weekDayBtns.forEach({$0.setTitleColor(UIColor.lightGrayColor(), forState: UIControlState.Normal)})
}
  1. 创建IBOutlet Collection@IBOutlet var weekDayBtns: [UIButton]!

  2. 创建一般IBAction@IBAction func weekDayBtnAction(sender: UIButton){}

  3. 将btns全部连接到IBOutlet CollectionIBAction
  4. 设置btn标记
  5. 这里有截图:

    enter image description here

    enter image description here

    enter image description here

答案 1 :(得分:0)

@Sauron:一个可能的解决方案是以下...... 如果每个按钮都有一个数字作为标题,你可以复制用于制作简单计算器的技术,其中... ...

  1. 第一个按钮用于创建IBAction / func(保持为uibutton)
  2. 然后将后续按钮按住Control键拖动到故事板的“文档大纲”层次结构中的“ViewController”
  3. 当弹出窗口显示时。转到中间,在“已发送事件”下,该事件应具有您创建的第一个按钮功能的名称
  4. 对所有其他按钮执行相同的操作
  5. 这有助于减少代码编写的重复。之后可以通过标题识别按钮。例如。

    @IBAction func numberTapped(sender: UIButton) {
    
        let nameOfDay = sender.currentTitle
    

    }

    因此允许您在代码中的其他地方使用工作日。 找到一个YouTube链接,可以帮助您快速重复:复制......?

    https://www.youtube.com/watch?v=NJHsdjH2HdY