【问题标题】:Radio Button Group Swift 3 Xcode 8单选按钮组 Swift 3 Xcode 8
【发布时间】:2017-12-03 23:46:42
【问题描述】:

我已经搜索了各种来源,但找不到一个清晰而简单的解决方案,可以在 Swift 3 中使用 Xcode 8.3 为 iOS 应用程序创建等效的单选按钮组。

例如,如果我在一组中有 3 个按钮,并且一次只能选择一个。目前,我通过在选择另一个按钮时将组中的 2 个按钮的状态更改为未选择来实现这一点,反之亦然。

@IBAction func buttonA(_ sender: Any) {
    buttonB.isChecked = false
    buttonC.isChecked = false
}

@IBAction func buttonB(_ sender: Any) {
    buttonA.isChecked = false
    buttonC.isChecked = false
}

@IBAction func buttonC(_ sender: Any) {
    buttonA.isChecked = false
    buttonB.isChecked = false
}

但是我希望有一种更有效的方法来做到这一点。

任何有关更有效解决方案的帮助将不胜感激。

【问题讨论】:

  • 您问题中的所有内容都与 macOS 有关,但您已将您的问题标记为 iOS。你是什​​么意思?
  • 我明白你的意思,我错误地混淆了文档。我的意思是iOS。需要明确的是,我正在努力寻找一种简单的方法来为 iOS 应用程序创建单选按钮组。
  • iOS 中没有原生单选按钮(或单选组)。您应该使用相关信息更新您的问题。

标签: ios swift radio-button xcode8 radio-group


【解决方案1】:

您可以将所有按钮的IBAction 连接到一个方法。

@IBAction func buttonClick(_ sender: UISwitch) { // you're using UISwitch I believe?

}

您应该将所有按钮添加到一个数组中:

// at class level
var buttons: [UISwitch]!

// in viewDidLoad
buttons = [buttonA, buttonB, buttonC]

然后,像这样编写buttonClick 方法:

buttons.forEach { $0.isChecked = false } // uncheck everything
sender.isChecked = true // check the button that is clicked on

替代方案:

尝试使用UITableView。每行包含一个选项。选择一行后,将该行的accessoryType 更改为.checkMark,每隔一行更改为.none

如果你太懒了,试试在cocoapods.org上搜索,看看其他人做了什么。

【讨论】:

  • 谢谢。我已经编辑了我原来的问题,以明确我指的是 iOS。事实上,我把文档弄混了。
【解决方案2】:

只需为所有三个按钮的 touchUpInside 事件创建一个选择器,并在您的 IB 中为正常状态设置 radio_off 图像,为选定状态设置 radio_on 图像,然后只需将 btnClicked 方法连接到所有按钮的 touchUpInside 事件

class ViewController: UIViewController {

@IBOutlet weak var btnFirst:UIButton!

@IBOutlet weak var btnSecond:UIButton!

@IBOutlet weak var btnThird:UIButton!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func btnClicked(sender:UIButton){

    let buttonArray = [btnFirst,btnSecond,btnThird]

    buttonArray.forEach{

        $0?.isSelected = false

    }

    sender.isSelected = true

}

【讨论】:

    【解决方案3】:

    根据您的 UI,您可以采用多种方法。

    UITableView - 使用带有复选标记装饰器的 UITableView。如果这些单选按钮的布局相当传统,那么这是正确的范例。如果布局是网格而不是列表,则可以使用UICollectionView

    您可以使用UITableViewDelegate 中的func table(_ table: WKInterfaceTable, didSelectRowAt rowIndex: Int) 来捕获选择。当您想要提交更改以确定选择了哪个单元格时,您可以在 tableView 上调用indexPathForSelectedRow

    Apple 的 UITableView 教程可以在以下位置找到:

    https://developer.apple.com/library/content/referencelibrary/GettingStarted/DevelopiOSAppsSwift/CreateATableView.html


    管理一组 UIButtons - 您可以存储对作为单选按钮组一部分的 UIButton 对象的引用数组。

    protocol RadioButtonDelegate: class {
        func didTapButton(_ button: UIButton)
    }
    
    class RadioButtonGroup {
        private var buttons: [UIButton] = []
        weak var delegate: RadioButtonDelegate?
    
        var selectedButton: UIButton? { return buttons.filter { $0.isSelected }.first }
    
        func addButton(_ button: UIButton) {
            buttons.append(button)
        }
    
        @objc private func didTapButton(_ button: UIButton) {
            button.isSelected = true
    
            deselectButtonsOtherThan(button)
    
            delegate?.didTapButton(button)
        }
    
        private func deselectButtonsOtherThan(_ selectedButton: UIButton) {
            for button in buttons where button != selectedButton {
                button.isSelected = false
            }
        }
    }
    
    class MyView: UIView {
        private var radioButtonGroup = RadioButtonGroup()
    
        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
    
            let button1 = UIButton(type: .custom)
            button1.setTitle("Eeeny", for: .normal)
            let button2 = UIButton(type: .custom)
            button2.setTitle("Meeny", for: .normal)
            let button3 = UIButton(type: .custom)
            button3.setTitle("Miny", for: .normal)
    
            self.radioButtonGroup.addButton(button1)
            self.radioButtonGroup.addButton(button2)
            self.radioButtonGroup.addButton(button3)
    
            addSubview(button1)
            addSubview(button2)
            addSubview(button3)
        }
    }
    

    【讨论】:

    • 感谢分享,但忘记添加到:button.addTarget(self, action: #selector(didTapButton(_:)), for: .touchUpInside)
    猜你喜欢
    • 2017-05-23
    • 1970-01-01
    • 2017-12-01
    • 1970-01-01
    • 2017-07-01
    • 2017-01-20
    • 2016-12-19
    • 1970-01-01
    • 2014-04-11
    相关资源
    最近更新 更多