Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在不单击的情况下更改swift中复选框的状态?_Swift_Macos_Checkbox_State_Nsbutton - Fatal编程技术网

如何在不单击的情况下更改swift中复选框的状态?

如何在不单击的情况下更改swift中复选框的状态?,swift,macos,checkbox,state,nsbutton,Swift,Macos,Checkbox,State,Nsbutton,例如,我有两个复选框,我的目标是如果选中复选框1,复选框2也会被选中。如果复选框1未选中,则复选框2也未选中。如何在不单击复选框的情况下更改复选框的状态?在UISwitch类中有一种方法: 例如: yourSwitch.setOn(true, animated: true) 为了将twi交换机相互链接,您必须编写更复杂的代码。如果在故事板中创建视图,代码将如下所示 import UIKit class ViewController: UIViewController { @IBOu

例如,我有两个复选框,我的目标是如果选中复选框1,复选框2也会被选中。如果复选框1未选中,则复选框2也未选中。如何在不单击复选框的情况下更改复选框的状态?

在UISwitch类中有一种方法:

例如:

yourSwitch.setOn(true, animated: true)
为了将twi交换机相互链接,您必须编写更复杂的代码。如果在故事板中创建视图,代码将如下所示

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)
    }
}

UISwitch类中有一个用于此的方法:

例如:

yourSwitch.setOn(true, animated: true)
为了将twi交换机相互链接,您必须编写更复杂的代码。如果在故事板中创建视图,代码将如下所示

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)
    }
}

将复选框与视图控制器连接,如下所示:

class ViewController: UIViewController {

  @IBOutlet weak var checkBox1: UISwitch!      
  @IBOutlet weak var checkBox2: UISwitch!

}
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)
    }
  }
}
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)
  }
}
class ViewController: NSViewController {
  @IBOutlet weak var checkBox1: NSButton!
  @IBOutlet weak var checkBox2: NSButton!
} 
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
    }
  }
}
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)
  }
}
然后将第一个复选框的iAction添加到视图控制器,同时使用if-else切换第二个复选框,如下所示:

class ViewController: UIViewController {

  @IBOutlet weak var checkBox1: UISwitch!      
  @IBOutlet weak var checkBox2: UISwitch!

}
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)
    }
  }
}
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)
  }
}
class ViewController: NSViewController {
  @IBOutlet weak var checkBox1: NSButton!
  @IBOutlet weak var checkBox2: NSButton!
} 
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
    }
  }
}
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)
  }
}
或者使用三元运算符,如:

class ViewController: UIViewController {

  @IBOutlet weak var checkBox1: UISwitch!      
  @IBOutlet weak var checkBox2: UISwitch!

}
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)
    }
  }
}
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)
  }
}
class ViewController: NSViewController {
  @IBOutlet weak var checkBox1: NSButton!
  @IBOutlet weak var checkBox2: NSButton!
} 
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
    }
  }
}
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)
  }
}
结果:

如果您不使用UISwitch,请使用当前代码更新您的问题

因OP的评论而更新:


将复选框与视图控制器连接,如下所示:

class ViewController: UIViewController {

  @IBOutlet weak var checkBox1: UISwitch!      
  @IBOutlet weak var checkBox2: UISwitch!

}
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)
    }
  }
}
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)
  }
}
class ViewController: NSViewController {
  @IBOutlet weak var checkBox1: NSButton!
  @IBOutlet weak var checkBox2: NSButton!
} 
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
    }
  }
}
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)
  }
}
然后将第一个复选框的iAction添加到视图控制器,同时使用if-else切换第二个复选框,如下所示:

class ViewController: UIViewController {

  @IBOutlet weak var checkBox1: UISwitch!      
  @IBOutlet weak var checkBox2: UISwitch!

}
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)
    }
  }
}
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)
  }
}
class ViewController: NSViewController {
  @IBOutlet weak var checkBox1: NSButton!
  @IBOutlet weak var checkBox2: NSButton!
} 
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
    }
  }
}
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)
  }
}
或者使用三元运算符,如:

class ViewController: UIViewController {

  @IBOutlet weak var checkBox1: UISwitch!      
  @IBOutlet weak var checkBox2: UISwitch!

}
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)
    }
  }
}
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)
  }
}
class ViewController: NSViewController {
  @IBOutlet weak var checkBox1: NSButton!
  @IBOutlet weak var checkBox2: NSButton!
} 
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
    }
  }
}
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)
  }
}
结果:


将复选框与视图控制器连接,如下所示:

class ViewController: UIViewController {

  @IBOutlet weak var checkBox1: UISwitch!      
  @IBOutlet weak var checkBox2: UISwitch!

}
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)
    }
  }
}
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)
  }
}
class ViewController: NSViewController {
  @IBOutlet weak var checkBox1: NSButton!
  @IBOutlet weak var checkBox2: NSButton!
} 
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
    }
  }
}
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)
  }
}
然后将第一个复选框的iAction添加到视图控制器,同时使用if-else切换第二个复选框,如下所示:

class ViewController: UIViewController {

  @IBOutlet weak var checkBox1: UISwitch!      
  @IBOutlet weak var checkBox2: UISwitch!

}
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)
    }
  }
}
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)
  }
}
class ViewController: NSViewController {
  @IBOutlet weak var checkBox1: NSButton!
  @IBOutlet weak var checkBox2: NSButton!
} 
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
    }
  }
}
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)
  }
}
或者使用三元运算符,如:

class ViewController: UIViewController {

  @IBOutlet weak var checkBox1: UISwitch!      
  @IBOutlet weak var checkBox2: UISwitch!

}
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)
    }
  }
}
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)
  }
}
class ViewController: NSViewController {
  @IBOutlet weak var checkBox1: NSButton!
  @IBOutlet weak var checkBox2: NSButton!
} 
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
    }
  }
}
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)
  }
}
结果:

如果您不使用UISwitch,请使用当前代码更新您的问题

因OP的评论而更新:


将复选框与视图控制器连接,如下所示:

class ViewController: UIViewController {

  @IBOutlet weak var checkBox1: UISwitch!      
  @IBOutlet weak var checkBox2: UISwitch!

}
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)
    }
  }
}
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)
  }
}
class ViewController: NSViewController {
  @IBOutlet weak var checkBox1: NSButton!
  @IBOutlet weak var checkBox2: NSButton!
} 
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
    }
  }
}
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)
  }
}
然后将第一个复选框的iAction添加到视图控制器,同时使用if-else切换第二个复选框,如下所示:

class ViewController: UIViewController {

  @IBOutlet weak var checkBox1: UISwitch!      
  @IBOutlet weak var checkBox2: UISwitch!

}
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)
    }
  }
}
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)
  }
}
class ViewController: NSViewController {
  @IBOutlet weak var checkBox1: NSButton!
  @IBOutlet weak var checkBox2: NSButton!
} 
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
    }
  }
}
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)
  }
}
或者使用三元运算符,如:

class ViewController: UIViewController {

  @IBOutlet weak var checkBox1: UISwitch!      
  @IBOutlet weak var checkBox2: UISwitch!

}
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)
    }
  }
}
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)
  }
}
class ViewController: NSViewController {
  @IBOutlet weak var checkBox1: NSButton!
  @IBOutlet weak var checkBox2: NSButton!
} 
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
    }
  }
}
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)
  }
}
结果:


谢谢你的快速回复。我得把我的问题写得更具体些。你的回答帮助了我,但我需要知道它在mac程序上是如何工作的,如果选中standart复选框,它会在其中打勾。我不使用女巫。我可以在NSButton和任何对象之间进行选择。更新了我的答案@feindpak谢谢你的快速回复。我得把我的问题写得更具体些。你的回答帮助了我,但我需要知道它在mac程序上是如何工作的,如果选中standart复选框,它会在其中打勾。我不使用女巫。我可以在NSButton和任何对象之间进行选择。更新了我的答案@Feindpak