ios - 如何让用户在具有多个 UIButton 的 View 中只选择一个自定义 UIButton? swift 3

标签 ios swift xcode uibutton

我在这里想做的是,我希望用户在具有多个 UIButton 的 View Controller 中选择一个自定义 UIButton,但是如果用户选择一个 UIButton,我如何才能做到这一点,如果他想要选择另一个,用户点击的第一个 UIButton 将被取消选择。

these are the UIButtons unclicked

these are the UIButtons clicked

如何让用户只选择一个 UIButton?如果一个 UIButton 被选中,另一个被选中,前者将被取消选中

最佳答案

所以你在按钮周围有某种边框,然后你可以遍历所有按钮并找到带有边框的按钮,将其删除,然后将边框设置为新选择的按钮。像这样:

func buttonSelected(clickedButton: UIButton) {
    for case let button as UIButton in self.view.subviews {
        if button.layer.borderColor == UIColor.black.cgColor {
            // deselect it here by changing the border
            button.layer.borderColor = UIColor.clear.cgColor
        }
        // select the clicked button
        clickedButton.layer.borderColor = UIColor.black.cgColor
    }
}

关于ios - 如何让用户在具有多个 UIButton 的 View 中只选择一个自定义 UIButton? swift 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43967662/

相关文章:

ios - 在 Firebase 中循环

iphone - 警告 : ld: warning: unexpected srelocation type 9 while building on device

ios - 如果用户拒绝相机访问,则无法显示警报

ios - 蓝牙通信可能性

ios - 这是什么 Apple Watch OS 警告 : "No custom interface defined for category "(null )". Will use system default presentation" mean?

ios - 在类 Initializer 中分配成员时在 Xcode 中向自身分配属性警告

ios - 如何在展开之前分配这些对象?

iphone - 导航 Controller 不旋转

ios - 为 iOS 应用程序提交开放图操作?

objective-c - 在 UIViewController 中设置 UIView 的最佳方式?