swift - 如何在同一 IBAction 中取消选择(取消突出显示)按钮

标签 swift xcode uibutton switch-statement ibaction

我在我的项目中使用了 3 个 UI 按钮,这 3 个按钮共享相同的 IBAction,并且我使用 Switch Controller 使用 Sender 提供的标签,使用 Sender.isSelected = True 突出显示发件人按钮。

我想做的是在每种情况下调用其他 2 个按钮来取消选择它们,我想知道是否有一种方法可以取消选择共享相同 IBAction 的其他按钮,而无需在每种情况下调用其他按钮.

我当前的代码:

@IBAction func tipChanged(_ sender: UIButton) { 

        switch sender.tag {
        case 0:
            tenPctButton.isSelected = false
            twentyPctButton.isSelected = false
            sender.isSelected = true

        case 10:
            zeroPctButton.isSelected = false
            twentyPctButton.isSelected = false
            sender.isSelected = true

        case 20:
            tenPctButton.isSelected = false
            zeroPctButton.isSelected = false
            sender.isSelected = true
     }
}

最佳答案

试试这个也许对你有帮助

  for view in self.view.subviews as [UIView] {
        if let btn = view as? UIButton {
             //here you can check your buttons tag and perform opration
        }
    }

关于swift - 如何在同一 IBAction 中取消选择(取消突出显示)按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59315269/

相关文章:

iOS:无法在 UITableViewCell 中使用 Activity Indicator

ios - 如何获取 TextField 输入以及为什么在我滚动时重复输入?

iphone - 如何制作像iOS一样的清晰按钮

swift - 即使在创建函数后,Swift 中的 UIButton 图像也不会改变颜色

ios - 在 UINavigationController 中推送 View Controller 时没有动画

ios - 如果他们仍然拒绝,请偶尔请求 iOS 权限

ios - 使用 IBAction 选择并按下表格单元格行

xcode - 你如何在 Swift 中导入 Metal?

ios - Xcode8 : Changing position of controls in storyboard View Controller has no effect on my app

ios - 当按下另一个 UI 按钮时隐藏一个 UI 按钮