ios - 两个按钮的背景颜色都改变了

标签 ios swift uibutton

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

@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)
        }
    }

最佳答案

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

@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)
        }
    }

关于ios - 两个按钮的背景颜色都改变了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49402885/

相关文章:

ios - 任何使用 EXT-X-MAP 标签的 m3u8 播放列表示例

ios - 修改 SwipeViewController Pod 以在 ContainerView 中运行

swift - Interface Builder 文件中的未知类 PFQueryTableView Swift/Parse 错误

swift - 如何对字符串日期数组进行排序

swift - private setter 和 readonly 属性有什么区别?

xcode - 在其他 .xib 中获取按钮标签

objective-c - Objective-C - 弹出 UITableViewController 场景时不释放内存

ios - 从 AppDelegate 在 WkWebView 中打开 url

ios - 无法以编程方式覆盖 UIButton 选项

ios - 具有单击和长按事件的 UIButton swift