首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在一个场景中以编程方式编码2 UISwitches

在一个场景中以编程方式编码2 UISwitches
EN

Stack Overflow用户
提问于 2020-02-26 10:26:48
回答 2查看 95关注 0票数 1

我正在编写一个应用程序,我正在使用Xcode 11.3.1 / Swift 5(尽管这个问题在Xcode 10/Swift 4中是相同的)。最初,当我开始编写我的应用程序时,我用故事板将两个UISwitches放在场景中,这个想法是,如果UISwit-1是打开的,那么UISwit-2就是关闭的,反之亦然。这个功能很好地发挥了作用(我的方法/代码/IBActions和IBOutlets) --所有这些都是有效的。

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

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

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

“”“

代码语言:javascript
复制
    // 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 .

“”“

代码语言:javascript
复制
 // 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)
        }
    }
'''
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-02-26 10:45:41

1.创建setSingleModesetFamilyMode作为ViewController的instance properties

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

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

以下是总结上述要点的代码。

代码语言:javascript
复制
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)
    }
}
票数 2
EN

Stack Overflow用户

发布于 2020-02-26 10:50:13

按以下方式切换代码:

代码语言:javascript
复制
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
}()

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

代码语言:javascript
复制
view.addSubView(setFamilyMode)
view.addSubView(setSingleMode)

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

别忘了为你的开关添加目标

代码语言:javascript
复制
setFamilyMode.addTarget(self, action: #selector(valueChange1(_:)), for: .valueChanged)
setSingleMode.addTarget(self, action: #selector(valueChange2(_:)), for: .valueChanged)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60411744

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档