Feindpak
Feindpak

Reputation: 45

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?

Upvotes: 1

Views: 2662

Answers (2)

ronatory
ronatory

Reputation: 7324

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

Upvotes: 5

Artem Stepanenko
Artem Stepanenko

Reputation: 3501

There's a method for this in the UISwitch class:

func setOn(Bool, animated: Bool)

Example:

yourSwitch.setOn(true, animated: true)

In order to link twi switches to each other you have to write something more sophisticated. If you create the view in a storyboard, the code would look somwhat like this.

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var firstSwitch: UISwitch!
    @IBOutlet weak var secondSwitch: UISwitch!

    @IBAction func firstValueChanged() {
        secondSwitch.setOn(firstSwitch.isOn, animated: true)
    }

    @IBAction func secondValueChanged() {
        firstSwitch.setOn(secondSwitch.isOn, animated: true)
    }
}

Upvotes: 1

Related Questions