使用Swift 4加载isOn或isOff用于UISwitch?

问题描述 投票:-2回答:1

我正在开发一个项目,我需要使用Swift 4将开关加载到.isOn或.isOff。我可以通过将开关设置为打开或关闭来在项目的主故事板中执行此操作,但我想要切换基于我的数据库中的每个用户的布尔值打开或关闭。

    //Create outlet for UISwitch
    @IBOutlet weak var switchBool1Outlet: UISwitch!

viewDidLoad() {
    //pulling data from Google Firebase to get true or false value    
    dataRef.reference().child("USERS/\(uid!)").observe(.value, with: { snapshot in
                    if let dictionary = snapshot.value as? [String: Any]
                    {
                        //MARK - Local Variables
                        var bool1Value = dictionary["bool1"] as! Bool

                        //trying to set switches here, this is not working
                        if bool1Value == true && bool1Value != nil {
                          //following throws error: "Expression resolves to an unused l-value"
                          self.switchBool1Outlet.isOn
                        }
        })
}

有关如何根据数据库中的true / false值启用/关闭UISwitch的任何想法?

swift xcode uiswitch
1个回答
1
投票

使用以下代码更改开关状态,请参阅here

self.switchBool1Outlet.setOn(true, 
  animated: true)
© www.soinside.com 2019 - 2024. All rights reserved.