How make radio buttons in swift 3

后端 未结 4 1768
闹比i
闹比i 2021-01-23 00:03

I am creating a Swift iOS app in which I have to show a Radio Buttons which accepts Horizontal and Vertical ways. I have found one library at GitHub which looks the solution of

4条回答
  •  余生分开走
    2021-01-23 00:51

    Here is less code solution I believe!

    @IBOutlet weak var btn1: UIButton!
    @IBOutlet weak var btn2: UIButton!
    .
    .
    .
    .
    @IBOutlet weak var btn10: UIButton!
    
    
    var arrButtons:[UIButton] = []
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            arrButtons.append(btn1)
            arrButtons.append(btn2)
            //so, on
        }
    

    Go to button property and set state Config property selected image for UIButton so, it will replace image when isSelected property is true.

    Also Bind touchUpInside event to every buttons with "btnRadioPrressed" method.

        @IBAction func btnRadioPrressed(sender: UIButton) {
    
            for btn in arrButons {
                btn.isSelected = false
            }
            if let index = arrButons.index(where: { $0 == sender }) {
                arrButons[index].isSelected = true
            }
        }
    

    Hope this will help you with less code.

提交回复
热议问题