简体   繁体   中英

how to create single selection check box in swift2?

import UIKit

class CheckBox: UIButton {
    // Images
    let checkedImage = UIImage(named: "check-sign.png")! as UIImage
    let uncheckedImage = UIImage(named: "blank-square.png")! as UIImage

    // Bool property
    var isChecked: Bool = false {
        didSet{
            if isChecked == true {
                self.setImage(checkedImage, forState: .Normal)
            } else {
                self.setImage(uncheckedImage, forState: .Normal)
            }
        }
    }

    override func awakeFromNib() {
        self.addTarget(self, action: "buttonClicked:", forControlEvents: UIControlEvents.TouchUpInside)
        self.isChecked = false
    }

    func buttonClicked(sender: UIButton) {
        if sender == self {
            isChecked = !isChecked
        }
    }
}

I was trying to implement checkboxes in Swift 2. I was referring to How to create radio buttons and checkbox in swift (iOS)? .

But my requirement is I want to implement single selection checkbox. I am not sure how to group the buttons via IB to make it single selectable.

Any suggestion?

Currently it is multiple selectable, I want to make it single selectable.

You can do it with UITableView and custom cells ,and change tableView.allowsMultipleSelection = flase.

Ex : You can do that by setting the accessoryType on the selected UITableViewCell instances to UITableViewCelAccessoryCheckmark.

To deselect the row, set it back to UITableViewCellAccessoryNone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM