【问题标题】:Both button background color changed两个按钮的背景颜色都改变了
【发布时间】:2018-03-21 09:45:15
【问题描述】:

当我使用该功能时,按钮无法正常工作。当我点击按钮时,按钮背景颜色会改变,当我们点击另一个按钮时,两个按钮的背景颜色都会改变。请告诉我如何解决这个错误。

@IBAction func btnNew(_ sender: Any)
    {
        if otlNewVisitor.isSelected == false
        {
            otlNewVisitor.isSelected == true
            Button.buttonPressed(button: otlNewVisitor, boolResult: true, titleColor: UIColor.white, strImage: "icn-new-visitor-wht", bgColor: UIColor(red: 30/255, green: 104/255, blue: 140/255, alpha: 1), imgVW: imgNewVis)

            //Entered SubmitDetails Screen
            let submitVC = self.storyboard?.instantiateViewController(withIdentifier: "SubmitDetailsVC") as! SubmitDetailsVC
            self.navigationController?.pushViewController(submitVC, animated: true)
        }
        else
        {
            otlNewVisitor.isSelected == false
            //Button Colow did change
            Button.buttonPressed(button: otlNewVisitor, boolResult: false, titleColor: UIColor(red: 30/255, green: 104/255, blue: 140/255, alpha: 1), strImage: "icn-new-visitor", bgColor: UIColor.white, imgVW: imgNewVis)
        }
    }

【问题讨论】:

  • 显示Button.buttonPressed的代码
  • 另外,您没有在比较后使用== 设置状态(可能您收到警告)

标签: ios swift uibutton


【解决方案1】:

您可以将您的代码更改为我的代码。仅当您在 if-else 条件中设置 button 的选定值时才会发生这种情况。因此,您想删除该代码。效果很好。

@IBAction func btnNew(_ sender: Any)
    {
        if otlNewVisitor.isSelected == false
        {
            Button.buttonPressed(button: otlNewVisitor, boolResult: true, titleColor: UIColor.white, strImage: "icn-new-visitor-wht", bgColor: UIColor(red: 30/255, green: 104/255, blue: 140/255, alpha: 1), imgVW: imgNewVis)

            //Entered SubmitDetails Screen
            let submitVC = self.storyboard?.instantiateViewController(withIdentifier: "SubmitDetailsVC") as! SubmitDetailsVC
            self.navigationController?.pushViewController(submitVC, animated: true)
        }
        else
        {
            //Button Colow did change
            Button.buttonPressed(button: otlNewVisitor, boolResult: false, titleColor: UIColor(red: 30/255, green: 104/255, blue: 140/255, alpha: 1), strImage: "icn-new-visitor", bgColor: UIColor.white, imgVW: imgNewVis)
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-25
    • 2017-03-25
    • 2015-09-09
    • 2020-03-10
    • 2015-10-29
    • 1970-01-01
    • 2015-06-04
    • 1970-01-01
    相关资源
    最近更新 更多