以编程方式在 1 个场景中编码 2 个 UISwitch

Programmatically coding 2 UISwitches in 1 Scene

我正在编写一个应用程序,我正在使用 Xcode 11.3.1 / Swift 5(尽管这个问题在 Xcode 10/Swift 4).最初,当我开始编写我的应用程序时,我使用情节提要将两个 UISwitch 放置在场景中,其想法是如果 UISwitch-1 打开,则 UISwitch-2 关闭,反之亦然,只要用户单击它们中的任何一个。该功能非常有用(我的 methods/code/IBActions 和 IBOutlets)- 一切正常。

但是,我想以编程方式对 UISwitch 进行编码而不是使用故事板,因为我希望能够根据 iPhone 设备类型将开关放置在 y 轴上的不同位置,即。因为 iPhone 11Pro Max 比 iPhone 6 长,所以我想将 UISwitch 向下移动。我不相信我可以在情节提要中做到这一点,因此想以编程方式对它们进行编码。

问题不在于开关的编码,我已经对它们进行了编码并且它们可以工作。但是问题是,如果我打开 UISwitch-1,那么我似乎无法理解关闭 UISwitch-2 的代码(反之亦然)。我认为我失败的地方是我不知道如何在方法中调用 IBOutlet 来调整 'other' 开关(即用户没有切换的开关)。

下面的代码块是使用 IBActions 和 IBOutlets 与情节提要 technique/method 关联的代码:

'''

    // MARK: My UISwitches - toggle between the two UISwitches
    @IBAction func singleUserModeSwitch(_ sender: UISwitch) {
        if dontAllowEditing == false {
            if (sender.isOn) == true {
                setFamilyMode.setOn(false, animated: true)
            } else {
                setFamilyMode.setOn(true, animated: false)
            }
        } else {
            myAlertNoDriverProfile()
        }
    }


    @IBOutlet weak var setFamilyMode: UISwitch!


    @IBAction func familyUserModeSwitch(_ sender: UISwitch) {
        if dontAllowEditing == false {
            if (sender.isOn) == true {
                setSingleMode.setOn(false, animated: true)
            } else {
                setSingleMode.setOn(true, animated: false)
            }
        } else {
            myAlertNoDriverProfile()
        }
    }


    @IBOutlet weak var setSingleMode: UISwitch!

'''

但是,下一个代码块是我尝试以编程方式对 UISwitch 进行编程时的代码,正如我所说,代码的工作原理是我可以显示 UISwitch,但是当我单击以编程方式编码的任一 UISwitch 时我无法让他们更新我以编程方式创建的其他 UISwitch....

'''

 // MARK: Display Switch Buttons
    func displaySwitches() {

        // Determine device type
        if DeviceType.isiPhone6 || DeviceType.isiPhoneX {
            singleUserModeSwitchPositionY = 484
            familyUserModeSwitchPositionY = 525
        }

        if DeviceType.isiPhone6Plus || DeviceType.isiPhoneXs {
            singleUserModeSwitchPositionY = 504
            familyUserModeSwitchPositionY = 545
        }

        // Display UISwitch-1
        let singleUserModeSwitch = UISwitch(frame:CGRect(x: 220, y:singleUserModeSwitchPositionY, width: 0, height:0))
        singleUserModeSwitch.isOn = true
        singleUserModeSwitch.thumbTintColor = UIColor.white
        singleUserModeSwitch.tintColor = Colors.jamesBlue
        singleUserModeSwitch.onTintColor = Colors.jamesBlue
        singleUserModeSwitch.setOn(false, animated: true)
        singleUserModeSwitch.addTarget(self, action: #selector(valueChange1), for:UIControl.Event.valueChanged)
        view.addSubview(singleUserModeSwitch)

        // Display UISwitch-2
        let familyUserModeSwitch = UISwitch(frame:CGRect(x: 220, y:familyUserModeSwitchPositionY, width: 0, height:0))
        familyUserModeSwitch.isOn = false
        familyUserModeSwitch.thumbTintColor = UIColor.white
        familyUserModeSwitch.tintColor = Colors.jamesBlue
        familyUserModeSwitch.onTintColor = Colors.jamesBlue
        familyUserModeSwitch.setOn(true, animated: true)
        familyUserModeSwitch.addTarget(self, action: #selector(valueChange2), for:UIControl.Event.valueChanged)
        self.view.addSubview(familyUserModeSwitch)
    }

    @objc func valueChange1(mySwitch1: UISwitch) {
        if mySwitch1.isOn{
            print("Switch1 (singleUserModeSwitch) is ON")
            // I think the lines below (where I try an call the .setOn function is where the problem is
            // as I somehow need to call an IBOULET however I don't have one defined because I am
            // programmatically creating the UISwitches
            setFamilyMode.setOn(false, animated: true)
        } else {
            print("Switch1 (singleUserMoeSwitch) is OFF")
            setFamilyMode.setOn(true, animated: true)
        }
    }

    @objc func valueChange2(mySwitch2: UISwitch) {
        if mySwitch2.isOn{
            print("Switch2 (familyUserModeSwitch) is ON")
            setSingleMode.setOn(false, animated: true)
        } else {
            print("Switch1 (singleUserMoeSwitch) is OFF")
            setSingleMode.setOn(true, animated: true)
        }
    }
'''

1. 创建setSingleModesetFamilyMode作为ViewController的instance properties

2.displaySwitches() 中将它们的值设置为 singleUserModeSwitchfamilyUserModeSwitch

3. 相应地修改 valueChange1(_:)valueChange2(_:) 定义。

这里是总结以上几点的代码。

class ViewController: UIViewController {
    var setSingleMode: UISwitch?
    var setFamilyMode: UISwitch?

    func displaySwitches() {
        //rest of the code...
        let singleUserModeSwitch = UISwitch(frame:CGRect(x: 220, y:singleUserModeSwitchPositionY, width: 0, height:0))
        //rest of the code...
        singleUserModeSwitch.addTarget(self, action: #selector(valueChange1(_:)), for:UIControl.Event.valueChanged)
        self.setSingleMode = singleUserModeSwitch

        let familyUserModeSwitch = UISwitch(frame:CGRect(x: 220, y:familyUserModeSwitchPositionY, width: 0, height:0))
        //rest of the code...
        familyUserModeSwitch.addTarget(self, action: #selector(valueChange2(_:)), for:UIControl.Event.valueChanged)
        self.setFamilyMode = familyUserModeSwitch
    }

    @objc func valueChange1(_ mySwitch1: UISwitch) {
        print("Switch1 (singleUserModeSwitch) is \(mySwitch1.isOn ? "ON" : "OFF")")
        setFamilyMode?.setOn(!mySwitch1.isOn, animated: true)
    }

    @objc func valueChange2(_ mySwitch2: UISwitch) {
        print("Switch2 (familyUserModeSwitch) is \(mySwitch2.isOn ? "ON" : "OFF")")
        setSingleMode?.setOn(!mySwitch2.isOn, animated: true)
    }
}

在代码中进行如下切换:

lazy var setFamilyMode: UISwitch = {
        let familySwitch = UISwitch()
        familySwitch.translatesAutoresizingMaskIntoConstraints = false
        // switch property settings
        return familySwitch
}()

lazy var setSingleMode: UISwitch = {
    let singleSwitch = UISwitch()
    singleSwitch.translatesAutoresizingMaskIntoConstraints = false
    // switch property settings
    return singleSwitch
}()

然后将开关添加到视图中:

view.addSubView(setFamilyMode)
view.addSubView(setSingleMode)

根据需要为开关添加约束或设置框架。如果您正在设置框架,请将开关的 translatesAutoresizingMaskIntoConstraints 属性 设置为 true。根据您的要求保存 yAxis 并根据您的代码进行更改。

不要忘记为您的开关添加目标

setFamilyMode.addTarget(self, action: #selector(valueChange1(_:)), for: .valueChanged)
setSingleMode.addTarget(self, action: #selector(valueChange2(_:)), for: .valueChanged)