Search code examples
swiftmacoscheckboxstatensbutton

How can i change the State of a checkbox in swift without clicking it?


For example, i have two checkboxes, and my goal is it if checkbox 1 is checked that checkbox 2 is checked too. and if checkbox 1 is unchecked that checkbox 2 is unchecked too. How can i change the state of a checkbox without clicking it?


Solution

  • Connect your checkboxes with the view controller like that:

    class ViewController: UIViewController {
    
      @IBOutlet weak var checkBox1: UISwitch!      
      @IBOutlet weak var checkBox2: UISwitch!
    
    }
    

    Then add an IBAction for your first checkbox to the view controller with using an if else to switch also the second checkbox, like that:

    class ViewController: UIViewController {
    
      @IBOutlet weak var checkBox1: UISwitch!
      @IBOutlet weak var checkBox2: UISwitch!
    
      @IBAction func checkBox1Pressed(_ sender: UISwitch) {
        // using if else
        if checkBox1.isOn {
          checkBox2.setOn(true, animated: true)
        } else {
          checkBox2.setOn(false, animated: true)
        }
      }
    }
    

    or using the ternary operator, like that:

    class ViewController: UIViewController {
    
      @IBOutlet weak var checkBox1: UISwitch!
      @IBOutlet weak var checkBox2: UISwitch!
    
      @IBAction func checkBox1Pressed(_ sender: UISwitch) {
        // or using ternary operator
        checkBox1.isOn ? checkBox2.setOn(true, animated: true) : checkBox2.setOn(false, animated: true)
      }
    }
    

    Result:

    enter image description here

    If you don't use UISwitch, please update your question with your current code

    Update due to comment from OP:

    Connect your checkboxes with the view controller like that:

    class ViewController: NSViewController {
      @IBOutlet weak var checkBox1: NSButton!
      @IBOutlet weak var checkBox2: NSButton!
    } 
    

    Then add an IBAction for your first checkbox to the view controller with using an if else to switch also the second checkbox, like that:

    class ViewController: NSViewController {
      @IBOutlet weak var checkBox1: NSButton!
      @IBOutlet weak var checkBox2: NSButton!
    
    
      @IBAction func checkBox1Pressed(_ sender: NSButton) {
        // Note: state checked == 1, state unchecked == 0
    
        // if checkBox1 is checked
        if checkBox1.state == 1 {
          // also set checkBox2 on checked state
          checkBox2.state = 1
        } else {
          // uncheck checkBox2
          checkBox2.state = 0
        }
      }
    }
    

    or using the ternary operator, like that:

    class ViewController: NSViewController {
      @IBOutlet weak var checkBox1: NSButton!
      @IBOutlet weak var checkBox2: NSButton!
    
    
      @IBAction func checkBox1Pressed(_ sender: NSButton) {
        // Note: state checked == 1, state unchecked == 0
    
        // or using ternary operator
        checkBox1.state == 1 ? (checkBox2.state = 1) : (checkBox2.state = 0)
      }
    }
    

    Result:

    enter image description here