ios - 以编程方式对 1 个场景中的 2 个 UISwitches 进行编码

标签 ios swift uiswitch programmatically

我正在编写一个应用程序,我正在使用 Xcode 11.3.1/Swift 5(尽管这个问题在 Xcode 10/Swift 4 中是相同的)。最初,当我开始编写我的应用程序时,我使用 Storyboard 将两个 UISwitches 放置在场景中,其想法是如果 UISwitch-1 打开,则 UISwitch-2 关闭,反之亦然,只要用户点击其中任何一个。该功能很有效(以及我的方法/代码/IBActions 和 IBOutlets) - 一切正常。

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

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

下面的代码块是与使用 IBActions 和 IBOutlets 的 Storyboard技术/方法相关的代码:

'''

    // 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!

'''

但是,下一个代码块是我尝试以编程方式对 UISwitches 进行编程时的代码,正如我所说的那样,代码的工作原理是我可以显示 UISwitches,但是当我单击我以编程方式编码的任何一个 UISwitches 时,我无法获得他们更新我以编程方式创建的另一个 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作为 instance properties的 View Controller 。

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

关于ios - 以编程方式对 1 个场景中的 2 个 UISwitches 进行编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60411744/

相关文章:

ios - Obj-C/Swift 项目中的致命陷阱异常

swift - 使用 UISwitch 设置初始化 Storyboard

ios - UISwitch - 检测动画结束

ios - 没有 "KeyboardName - HostAppName"方案的自定义键盘名称

ios - 如果 tableView 数据为空,如何显示另一个 View Controller

ios - 'CGFloat' 不可转换为 'Float' 及更多

swift - 如何关闭 UITableview 中除我单击的开关之外的所有其他开关

iOS - 访问模态的父属性

iphone - 自定义 UITextView 占位符在键盘输入时不会删除

swift - iPhone X 上的节点定位