ios - 在 Button 中,罢工越来越好,但无法取消罢工
问题描述
在 UIButton 上单击一对按钮将触发并单击另一个按钮,罢工不会被取消是问题,罢工功能工作正常并且在没有得到....
代码:取消罢工
func updAvaTime() {
upAvailableTime(sender:time805Btn)
upAvailableTime(sender:time830Btn)
upAvailableTime(sender:time905Btn)
upAvailableTime(sender:time930Btn)
upAvailableTime(sender:time1005Btn)
upAvailableTime(sender:time1030Btn)
upAvailableTime(sender:time115Btn)
upAvailableTime(sender:time1130Btn)
upAvailableTime(sender:time125Btn)
upAvailableTime(sender:time1230Btn)
upAvailableTime(sender:time105Btn)
upAvailableTime(sender:time130Btn)
upAvailableTime(sender:time205Btn)
upAvailableTime(sender:time230Btn)
upAvailableTime(sender:time305Btn)
upAvailableTime(sender:time330Btn)
upAvailableTime(sender:time405Btn)
upAvailableTime(sender:time430Btn)
upAvailableTime(sender:time505Btn)
upAvailableTime(sender:time530Btn)
upAvailableTime(sender:time605Btn)
upAvailableTime(sender:time630Btn)
upAvailableTime(sender:time705Btn)
upAvailableTime(sender:time730Btn)
}
func upAvailableTime(sender: UIButton) {
let currentTime = sender.currentTitle//(for: .normal)!
let attrString: NSMutableAttributedString = NSMutableAttributedString(string: currentTime!)
attrString.removeAttribute(NSAttributedString.Key.strikethroughStyle , range:NSMakeRange(0, attrString.length))
}
解决方案
为 UIButton 设置罢工和取消罢工属性
let attributesUnstrike = [NSAttributedString.Key.strikethroughStyle : 0]
let titleUnselected = NSAttributedString(string: "YOUR STRING", attributes: attributesUnstrike)
//Set strike for selected state
let attributesStrike = [NSAttributedString.Key.strikethroughStyle : 1]
let titleSelected = NSAttributedString(string: "YOUR STRING", attributes: attributesStrike)
btn.setAttributedTitle(titleUnselected, for: .normal)
btn.setAttributedTitle(titleSelected, for: .selected)
@IBAction func reloadtable(sender:UIButton){
sender.isSelected = !sender.isSelected
//Automatically change Button state automatically based on curent state
}
更新:
time805Btn.isSelected = true // To Strike
time805Btn.isSelected = false // To Unstrike
注意:确保您的
UIButton
类型应该是自定义的。
推荐阅读
- android - Android Studio 将 androidx 迁移到 android 8.1
- ruby-on-rails - 给定工件 url 从工件读取校验和
- docker - 在带有gpu的docker内部使用tensorflow
- google-cloud-storage - list_blobs_with_prefix() 带有前缀参数的正则表达式
- r - 通过 mutate case_when 通过多个条件创建新变量
- javascript - 我如何选择与猫鼬的收藏?
- python - 有没有办法从我的 Python 脚本中获取所需的确切数据
- google-apps-script - 在不同工作表上的 2 个值之间搜索匹配项,并使用 google app 脚本将另一列中的数据检索到第一张工作表中
- r - R - dplyr。具有类似于数据框列的变量的函数
- javascript - Angular:如何在 Dragula ng2 上动态更改放置的元素?