我有现在的代码,允许选择重复事件的星期几。目前它们是独立的,重复性很高。
如何重构按钮以减少重复?
// 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)
}
答案 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 :(得分:0)
这有助于减少代码编写的重复。之后可以通过标题识别按钮。例如。
@IBAction func numberTapped(sender: UIButton) {
let nameOfDay = sender.currentTitle
}
因此允许您在代码中的其他地方使用工作日。 找到一个YouTube链接,可以帮助您快速重复:复制......?