首页 > 解决方案 > 在 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))

}

在此处输入图像描述

标签: iosswiftbutton

解决方案


为 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类型应该是自定义的。


推荐阅读