gpt4 book ai didi

ios - 合并按钮操作以减少重复

转载 作者:行者123 更新时间:2023-11-29 00:57:11 25 4
gpt4 key购买 nike

我有现在的代码,可以选择一周中的哪几天重复事件。目前它们是独立的,重复率高。

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

// 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)

}

最佳答案

试试下面的代码:

@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集合:@IBOutlet var weekDayBtns: [UIButton]!

  2. 创建通用 IBAction :@IBAction func weekDayBtnAction(sender: UIButton){}

  3. 将 btns 全部连接到 IBOutlet CollectionIBAction
  4. 设置btn标签

截图如下:

enter image description here

enter image description here

enter image description here

关于ios - 合并按钮操作以减少重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37514701/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com