How to Mark or Uncheck CheckBox?

2

I need help with a custom checkbox.

To make them I put two buttons and create two classes, one for each checkbox that gives them their form which I take from this same site, I leave the class with which I give shape to the button

import UIKit

class CheckBox: UIButton {
// Images
let checkedImage = UIImage(named: "ic_check_box")! as UIImage
let uncheckedImage = UIImage(named: "ic_check_box_outline_blank")! 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
    }
}
}

What I need is a guide so that when a checkbox is marked the other one is unmarked, I leave a guide image

    
asked by elkin sepulveda 01.08.2016 в 00:33
source

2 answers

0

If you use a custom class to check, you can only control your bone status to be the button, what you should do to control that if you have a button marked the other is unchecked, is to simply create a function that checks that if you have pressed on the YES, uncheck the No and vice versa.

    
answered by 01.08.2016 в 10:22
0

Why not use a library that already does it? Try CheckboxButton from Chris Amanse is very simple and does exactly what you need:

CheckboxButton changes state each time an event .TouchUpInside occurs. And you can record the status changes using the UIControlEvents.ValueChanged event.

The validations are as simple as:

if checkbox.on {
  print("Checkbox is checked")
}

// Toggle
checkbox.on = false // Set to false to uncheck

You can install it with Carthage by adding github "chrisamanse/CheckboxButton" to your Cartfile , or manually by simply copying the CheckboxButton.swift file to your project.

    
answered by 02.08.2016 в 19:35